Merge statement error in SQL Server 2008 - sql

I am executing the following merge statement in SQL Server 2008:
MERGE
PopulationData AS a
USING ImagesData AS b
ON a.ID = b.ID
WHEN MATCHED THEN
UPDATE SET a.SURNAME = 'joe123'
WHEN NOT MATCHED THEN INSERT(a.ID,a.SURNAME)
VALUES (12454,'joe123');
I have the following error:
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'AS'.
Can anyone tell me where the syntax error.

Parsing your query in SQL Management Studio gives me the following error:
Msg 10739, Level 15, State 1, Line 7 The insert column list used in
the MERGE statement cannot contain multi-part identifiers. Use single
part identifiers instead.
I then remove the identifiers...
MERGE
PopulationData AS a
USING ImagesData AS b
ON a.ID = b.ID
WHEN MATCHED THEN
UPDATE SET a.SURNAME = 'joe123'
WHEN NOT MATCHED THEN INSERT(ID,SURNAME)
VALUES (12454,'joe123');
...and the query parses successfully. Therefore, the syntax error is almost certainly not coming from your MERGE statement. Are you really executing only the statement you posted, or is it part of a larger script or procedure? And if you double-click the error message, it should highlight the line where the syntax error is (at least with SQL 2008).
Update: I noticed that you have tagged the question for SQL 2005 and 2008, but MERGE is only supported in SQL 2008. Parsing the query under SQL 2005 gives the syntax error.

the problem that i was executing the query on sql server management studio 2008 but i was connecting to sql server 2005 database on another server. Now it is fixed

Related

Getting an error when trying to merge missing items between two databases on SQL Server 2005

So, kind of an SQL Newbie here and I am trying to get something to work on Microsoft SQL Server 2005 (yay for old outdated databases powering businesses still).
I can get it to work properly on my local dev machine (running SQL Server 2019) but when I run it on the 2005 server it errors out.
Query:
MERGE CustomDB.[dbo].StockCounts AS [Target]
USING (SELECT ID,
ProductNo
FROM CompanyDBReplication.[dbo].STOCKPRODUCT) AS [Source] (ID,
ProductNo)
ON [Target].ID = [Source].ID
WHEN NOT MATCHED THEN
INSERT (id,
ProductNo,
CountDate,
CountID)
VALUES ([Source].ID,
[Source].ProductNo,
NULL,
NULL);
Error:
Msg 102, Level 15, State 1, Line 1 Incorrect syntax near '.'.
Msg 156, Level 15, State 1, Line 4 Incorrect syntax near the keyword 'AS'.
Now I don't know enough about the differences here of why this would error out nor how I would go about searching this (I really don't do SQL ever and sort of had to Google this to make it work in the first place).
Basically I want to copy/merge items from a source database into the target database and add new ones that might get added to the source if they are not found in the target.
If someone can help me either fix this one to work on SQL Server 2005 or propose/give me an example of a different solution that will accomplish the same thing and work on SQL Server 2005 that would be awesome and I would forever be indebted.
The source of where I got this solution is here: https://stackoverflow.com/a/34892729/5877943
Merge statement appeared only in MSSQL 2008, you can use an outer join instead.
Something like this.
INSERT INTO CustomDB.[dbo].StockCounts (
id,
ProductNo,
CountDate,
CountID
)
SELECT
[Source].ID,
[Source].ProductNo,
NULL,
NULL
FROM CompanyDBReplication.[dbo].STOCKPRODUCT) AS [Source]
LEFT JOIN CustomDB.[dbo].StockCounts AS [Target] ON [Target].ID = [Source].ID
WHERE [TARGET].Id IS NULL;

Can't insert multiple rows in SQL Server Manager Express 2005

I have a Table HORAS_X with ID_HORA(int), ID_ZONA(int), DESCRIPCION(nvarchar), COMIDA(bit), META(int), NUMERO(int).
I can insert a single row like:
INSERT INTO HORA_X
(ID_HORA,ID_ZONA,DESCRIPCION,COMIDA,META,NUMERO)
VALUES
(2,2,'06:00-07:00',0,174,1);
And it works.
However when I try to insert multiple rows like this:
INSERT INTO HORA_X
(ID_HORA,ID_ZONA,DESCRIPCION,COMIDA,META,NUMERO)
VALUES
(3,3,'06:00-07:00',0,174,1),
(4,4,'06:00-07:00',0,174,1);
It throws the error
Msg 102, Level 15, State 1, Line 2 Wrong syntax near ','.`
Is my Syntax wrong? I checked online and it's supposed to be fine.
And yes, I have restarted SQL Server Manager, thanks for any lead and help.
If I remember correctly, SQL Server 2005 does not support VALUES table value constructor. It was introduced in SQL Server 2008, so for SQL Server 2005 you need to use the following statement:
INSERT INTO HORA_X (ID_HORA,ID_ZONA,DESCRIPCION,COMIDA,META,NUMERO) VALUES (3,3,'06:00-07:00',0,174,1)
INSERT INTO HORA_X (ID_HORA,ID_ZONA,DESCRIPCION,COMIDA,META,NUMERO) VALUES (4,4,'06:00-07:00',0,174,1);
Notes: SQL Server Manager Express 2005 is a tool, but I assume, that you are using SQL Server 2005.

column names with periods on a 2008 linked server

I'm using SQL Server 2005 and trying to select columns from a SQL Server 2008 linked server that have periods in them.
I've checked this post:
Selecting a column with period in the column name SQL Server
But I'm not getting the same error.
This is the code
INSERT INTO [Linked_Server].Database.dbo.Table
([Column_Name], [Column.Name])
SELECT
[Column_Name], [Column.Name] FROM local_table
I can select from the table locally, but when I try to select the same columns from the same table on the linked server, I get this error:
Msg 207, Level 16, State 1, Line 3
Invalid column name 'Column.Name'.
EDIT: Fixed a typo with "[Column_Name)". This was a typo only in stackoverflow however.
The typo is not the problem
Replace ) on ] after Column_Name and try once again
INSERT INTO [Linked_Server].Database.dbo.Table
([Column_Name], [Column.Name])
SELECT [Column_Name], [Column.Name] FROM local_table
Looks like this is a known issue with Linked Servers in SQL Server 2008. I had seen this page while I was researching, but I didn't see the workaround section at the bottom.
I ended up changing the query to use OPENQUERY instead, and now it works.
http://support.microsoft.com/kb/972856

Can a fake table be created in SQL Server 2005

I would like to know if it is possible to create a fake table in SQL Server 2005.
Because when I tried I got an error
Msg 208, Level 16, State 1, Line 1
Invalid object name 'dual'.
This is what I tried
and it did not work. For test purpose I have created a test table to execute the query.
In SQL Server you are allowed to leave out the FROM clause. So you don't need a fake table.
Instead of writing
select 42
from dual;
just write
select 42;

Retrieve the last inserted record's ID in SQL Server CE 4

I am trying to implement the good ol' SELECT ##IDENTITY like:
INSERT INTO NewsCategories (nCatName)
VALUES (#nCatName);
SELECT ##IDENTITY AS NewID;
But my SQL Server CE 4 is giving me the error:
There was an error parsing the query. [ Token line number = 4,Token
line offset = 1,Token in error = SELECT ]
Is this something to do with the limitations of SQL Server CE? If so are there any workarounds for this?
Sql Server Compact Edition doesn't support batched queries, but you should be able to execute them one at a time and get what you're after.