SQL Server 2008 - Merge Script not working - sql

This merge script i'm writing isn't compiling and I believe I have the correct syntax.
MERGE into MyTable ct_current
USING (SELECT '0%' as Description, '0' as ShareAmount) ct_value
ON ct_current.ShareAmount = ct_value.ShareAmount
WHEN MATCHED THEN
UPDATE SET ct_current.Description = '0%'
WHEN NOT MATCHED THEN
INSERT (Description, ShareAmount)
VALUES (ct_value.Description, ct_value.ShareAmount);
GO
Error:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'into'.
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near 'ct_value'.

Try
MERGE MyTable AS ct_current
USING (SELECT '0%' as Description, '0' as ShareAmount) ct_value
ON ct_current.ShareAmount = ct_value.ShareAmount
WHEN MATCHED THEN
UPDATE SET ct_current.Description = '0%'
WHEN NOT MATCHED THEN
INSERT (Description, ShareAmount)
VALUES (ct_value.Description, ct_value.ShareAmount);

I realized that I had the SQL Server 2008 R2 client, but the server is SQL Server 2005.

Related

SQL Server: incorrect syntax near the keyword

I am trying to run the following T-SQL code which contains 8160 individual numbered accounts and receive the the following errors, with Line 10 being the first instance of INSERT and line 8162 being the last instance of INSERT
Code:
SELECT
AXACNO AS [AGREEMENT],
AX15C1 AS [BLOCKCODE]
FROM
BIDW_Staging.BPFCentrac.ACCEXTPF
WHERE
AXACNO IN (
INSERT INTO ##AN_INSERT VALUES ('0000000000000000')
INSERT INTO ##AN_INSERT VALUES ('1111111111111111')
INSERT INTO ##AN_INSERT VALUES ('2222222222222222')
I get these errors:
Msg 156, Level 15, State 1, Line 10
Incorrect syntax near the keyword 'INSERT'
Msg 102, Level 15, State 1, Line 8162
Incorrect syntax near ')'
First you need to insert your data into your ##AN_INSERT table. Each INSERT is a separate statement and should be terminated with a ;.
INSERT INTO ##AN_INSERT VALUES ('0000000000000000');
INSERT INTO ##AN_INSERT VALUES ('1111111111111111');
INSERT INTO ##AN_INSERT VALUES ('2222222222222222');
Then you can select using this table:
select
AXACNO AS [AGREEMENT],
AX15C1 AS [BLOCKCODE]
from BIDW_Staging.BPFCentrac.ACCEXTPF
Where AXACNO in
(SELECT <whatevercolumnthisis> FROM ##AN_INSERT);

How do I do select into from another table

Basically I want to run a SQL statement like this, but I can't due to errors.
Not sure why?
INSERT INTO allevent (eventname)
VALUES (select username from registered)
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'select'.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ')'.
No need of parentheses around the SELECT statement in your query.
INSERT INTO allevent (eventname)
SELECT username FROM registered
Parentheses with VALUES required, where some places like below:
INSERT INTO tablename (fieldname)
VALUES ('field 01'), ('field 02');
Please find the syntax for the INSERT statement.

Create a table with SQL from Query result in SSMS

I am trying to create a new table from a query result in SSMS
I have tried:
CREATE TABLE table2 AS
(SELECT * FROM table1
WHERE code = 'x'
ORDER BY code;)
However I am getting an error telling me:
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'AS'.
Msg 156, Level 15, State 1, Line 8
Incorrect syntax near the keyword 'ORDER'.
Can anyone help?
Try this:
SELECT *
INTO NEW_TABLE
FROM table1
WHERE code = 'x'
ORDER BY code;

Insert statement in SQL Server 2005

I have strange problem related to SQL Server 2005
When I try to insert into table
insert into IDName
VALUES (101 , 'AA'),
(301 , 'BB')
I receive this error
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near ','.
There is no problem, if I insert records one by one.
EDIT:
Thanks for reply guys.... but this script works in other installation of sql server 2005... I think it is some setting issue but i do not where... if you could help
This syntax was introduced in SQL Server 2008. So upgrade, or use the more verbose:
INSERT dbo.IDName(column1, column2)
SELECT 101 , 'AA'
UNION ALL SELECT 301 , 'BB';
Some additional changes:
Always use the schema prefix when referencing objects
Always specify the column list for an INSERT
Always use semi-colons to terminate statements
SQL Server 2005 does not support that insert syntax; you would either need
insert into IDName
SELECT 101 , 'AA'
UNION ALL SELECT 301 , 'BB'
or
insert into IDName VALUES (101 , 'AA');
insert into IDName VALUES (301 , 'BB');

Msg 102, Level 15, State 1, Line 2 Incorrect syntax near ',' trying to INSERT INTO

i've succesfully created db and tables, but when i try populate one of a table, like this
INSERT INTO Products(IsProductActive,ProductName,ProductCount)
VALUES(0,'productName1',0),
(0,'productName2',0),
(1,'productName3',9),
(1,'productName4',7),
(1,'productName5',3),
(1,'productName6',10),
(0,'productName7',0),
(1,'productName8',6),
(1,'productName9',12),
(1,'productName10',20);
GO
i got an error:
Msg 102, Level 15, State 1,
Line 2 Incorrect syntax near ','.
firstly which ',' is meant, secondly - what is wrong?
PS: i use MS Management Studio v 9.0 if it is needed...
Versions of SQL Server 2005 and below do not support the multiple VALUE clause syntax
SQL Server 2005 is version 9...
See How do I insert multiple rows WITHOUT repeating the "INSERT INTO dbo.Blah" part of the statement? for more
if you are using SQL SERVER 2005 and below, the query wouldn't work because it doesn't support multiple value clause insert statement. You should insert it one by one.
Like this one below,
INSERT INTO Products(IsProductActive,ProductName,ProductCount)
VALUES(0,'productName1',0)
GO
INSERT INTO Products(IsProductActive,ProductName,ProductCount)
VALUES(0,'productName2',0)
GO