I get this error "Optimistic concurrency control error" when I want edit a SQL Server view - sql

I have two tables table_1 and Table_2; I created this view in SQL Server:
SELECT dbo.Table_1.ID, dbo.Table_1.Title, dbo.Table_2.Descc
FROM dbo.Table_1
LEFT OUTER JOIN dbo.Table_2 ON dbo.Table_1.ID = dbo.Table_2.ID
But when I want to enter data into Descc column, I get an error
Optimistic concurrency control error

This issue occurs if the following conditions are true:
The table contains one or more columns of the text or ntext data type.
The value of one of these columns contains the following characters:
Percent sign (%)
Underscore (_)
Left bracket ([)
The table does not contain a primary key.
Note This issue also occurs when you try to use Table Designer in Microsoft Visual Studio 2005 to update a table that is in a SQL Server 2005 database.
Workaround
To work around this issue, create a new query window in SQL Server Management Studio. Then, run a SQL UPDATE statement to update the row in the table.
Note If you receive the first error message that is mentioned in the "Symptoms" section, you can click Yes to update the row.
microsoft link

Related

SQL (DB2) Creating table by selecting from other table

I´ve read several discussions and websites about creating a SQL query to create a table by selecting data from other table, but none of them solved my issue. I don´t know what else to do.
I am working on a SQL script to run a sequence of selects and creates do resume some data. I´ve been using this SQL queries on a SaS server using DB2 data. Now I need to migrate to Dbeaver to using other sources.
I just want to create a table by selecting some columns and data from other table, for this simple example :
"CREATE TABLE DB2XXXX.PaidResume AS
(SELECT HistoricalPaid.AccountNumber AS CONTA
FROM DB2XXX.HistPaid HistoricalPaid
WHERE HistoricalPaid.AccountNumber = 'XXXXX');
All I got it this error
Error occurred during SQL query execution
SQL Error [42601]: ILLEGAL SYMBOL "<END-OF-STATEMENT>". SOME SYMBOLS THAT MIGHT BE LEGAL ARE:. SQLCODE=-104, SQLSTATE=42601, DRIVER=4.19.26
If I just exclude the "CREATE TABLE DB2XXXX.PaidResume AS" and run the select only, it works.
You must include WITH DATA or WITH NO DATA at the end of the SQL statement. For example:
create table u as (select a from t) with data;
See example at db<>fiddle.

Adding a Computed column with After clause

I am trying to create a computed column after a specified column.
But the SQL gives syntax error for the query below. Please help me with the correct syntax/way to do this.
ALTER TABLE service_ServiceClass
ADD LichenClassName AS ([dbo].[UfnGetServiceClassName]([Id])) AFTER Description
You get an error because "AFTER" is MySQL syntax.
There is no direct way to alter column order in SQL Server currently.
In MS SQL Server Management Studio, you'd have to use the SSMS Table Designer (rightclick yourTable->Design) to arrange your columns, which can then generate a script which drops and recreates the table.
There is no after clause in sql server's alter table statement.
The order of the columns in the table (as well is the order of the rows, btw) is completely insignificant from a user's point of view.
It's significant from the server point of view, as it may effect performance when using clustered indexes.
The only time the order of the columns matters is when you are using select * ..., but you shouldn't use that anyway. Always specify the column names directly.

How to edit SQL records using Select statement

I got like 2000 rows of data, when I do select statement I can narrow my query but I cant change any value, as I have to change 50 columns, I dont want to use Update Command as then because values I have to update for each row is unique.
Anyone knows any easier way of selecting data using select command and then edit it.
EDIT
I just went to SQL Management studio, clicked on DB I am working on then find the table, I right clicked on it and then it says "Select top 1000 rows" so Now I can see the query and the data, I added "Where" into query and got what I wanted, but I cant modify the table rows below :S
*Edit 2 *
Or I can develop a Utility that will take Table Name , Column Name and its New value and simply updates it :)
You modify data using an UPDATE statement (with a WHERE clause) in a query window. While Management Studio has a feature called "Edit Top n Rows" that doesn't mean it's a good idea to use it - there are several behavioral bugs that are still unresolved even in the SQL Server 2012 version, and it can also place unnecessary and prohibitive locks on the underlying table.
I know it's not the answer you want to hear, but please become comfortable with proper DML commands. The documentation for UPDATE is found here:
http://msdn.microsoft.com/en-us/library/ms177523(v=sql.100).aspx
The long and short of it, IMHO: If you can identify the rows you want to update by using a SELECT with a WHERE clause, you can also write an UPDATE query using the same WHERE clause.
In SSMS - in the same place you saw "Select top 1000 rows", there should be another option "Open table" or "Edit top 200 Rows" (by default).
If you did that on a table, then pressed the following button:
You should then by able to paste in your select statement you've already written instead of what is there, press the execute button (Red exclamation mark button). The grid that appears is editable.
In general I'd be inclined to agree with #AaronBertrand though, it's better to learn the syntax to Update statements yourself.
If you have Microsoft Access, you can create a linked table to your sql server database and edit it by opening the table and modifying the relevant rows. You can create a query in Microsoft Access as well limiting the rows you want to return, and then you can inline edit the columns you'd like to change in the result set.
See for example here how to create a linked table to SQL Server.

ClientDataset.RefreshRecord no longer works in Delphi XE for joined tables - any workarounds?

TClientDataset.RefreshRecord no longer generates the table join part of SQL when trying to refresh a record on a ClientDataset connected to a dataset with a joined table in the SQL statement.
As a result, calling this method results in SQL error "invalid column names" for each field not in the main table.
This was not a problem in Delphi 2010 and earlier.
The error occurs with both DBX4 or BDE components connected to the TClientDataset and thus it is highly likely the issue is a problem with changes to TClientDataset code.
To replicate this problem:
Create a new app in Delphi XE with only a single form and drop the required database components on it (TSQLMonitor, TSQLConnection, TSQLQuery, TDatasetProvider, TClientDataset, TDatasource, and TDBGrid) and bind them to each other.
Created a simple SQL statement with a table join and placed this in the TSQLDataset.SQL property.
The SQL statement included just two fields - the key field of the main table, and a field from a joined table - for example in pseudocode:
Select
MainTable.IntegerKeyField
, JoinedTable.JoinField
FROM MainTable
LEFT OUTER JOIN JoinedTable ON MainTable.LookupFieldID = JoinedTable.JoinKeyField
Add both of these fields as persistent fields in both TSQLQuery and TClientDataset with Provider Flag for the key field including pfInKey (RefreshRecord will not work if it does not know which field is the key hence persistent fields is a must).
Add two buttons on the form - one just opens the Clientdataset and the 2nd button calls clientdataset.refreshrecord;
Run the app, press button to open dataset and data displays in the grid.
Press the Refresh Record button, and you will get SQL error "invalid column name" for the joined field.
Close the app, open the SQLMonitor log and in the refresh record SQL statement that Delphi generated, you will see it has not included the table join statement.
====
I would really appreciate any ideas on how to fix this.
Try using a view on the database to implement the required join. Then the delphi component can just select from view_name rather than have to handle the join itself.

Is there a web site where I can paste a SQL Insert statement and have it break it out by column?

I've been working on an application that has no discernable data access layer, so all the SQL statements are just built up as strings and executed. I'm constantly confronted by very long INSERT statements where I'm trying to figure out what value in the VALUES list matches up with what column in the column name list.
I was about to create a little helper application where I could paste in an INSERT statement and have it show me a list of values matched up with the column names, just for debugging, and I thought, "someone else has probably done this already."
Does anyone know of a web site where I can just paste in an INSERT statement and have it show me a two column table with column names in the first column and values in the second column?
not a website but have you tried either visual studio or Sql Server Management studio. Both of these are able to do this.
You can use SQL Server Management studio or Visual Studio (with Server Explorer) to do that. For SSMS, follow these steps:
On a random table, click "Edit Top 200 Rows"
Open the SQL pane and paste your Insert script.
Open the Criteria Pane, which displays the insert column value pairs.
http://www.dpriver.com/pp/sqlformat.htm
I use Red Gate SQL Prompt for all my re-formatting.
Even though it is unnecessary (and unused), I often use aliases to make it easier to line things up, so instead of:
INSERT INTO tbl
SELECT 2 * y
FROM other_tbl
I usually do:
INSERT INTO tbl (x)
SELECT 2 * y AS x
FROM other_tbl