Insert statement in SQL Server 2005 - sql

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');

Related

Subquery cannot appear in an Insert Values statement

I have the following query in SQL Server CE which gives me an error during the execution time:
INSERT INTO trans_rel(trans, sale_purch_id, inc_exp_id)
VALUES('p', 104, (select MAX(expence_id) from c_expence))
The error is this:
Subquery cannot appear in an Insert Values statement.
What is wrong with this query?
Try this one:
INSERT INTO trans_rel
SELECT 'p', '102', MAX(expence_id)
FROM c_expence
This is exactly what your are looking for:
INSERT INTO trans_rel(trans, sale_purch_id, inc_exp_id)
SELECT 'p' as 'trans', '104' as 'sale_purch_id', MAX(expence_id) AS inc_exp_id
FROM c_expence;

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

SQL Server – inserting multiple rows with single (ANSI style) statement

I am using following method for inserting multiple rows using a single INSERT statement, that is the ANSI style of inserting rows. It is available in SQL Server 2008 and 2012. I am not sure of SQL Server 2005/ 2000.
Create test table:
create table TestInsert (ID INT, Name NVARCHAR(50))
Single INSERT statement to insert 5 rows
INSERT INTO TestInsert
VALUES (1,'a'),
(2,'b'),
(3,'c'),
(4,'d'),
(5,'e')
Please let me know if there is any other best way to achieve this
Try this instead:
INSERT TestInsert
SELECT 1, 'a'
UNION ALL
SELECT 2, 'b'
UNION ALL
SELECT 3, 'c'
UNION ALL
SELECT 4, 'd'
UNION ALL
SELECT 5, 'e'
SQL Server - inserting multiple rows with single (ANSI style) statement
For SQL Server 2000+
According to SQL The Complete Reference, Third Edition (August 12, 2009):
1) The syntax for multirow INSERTs is
INSERT INTO table-name (columns not mandatory)
query
(page 236, Figure 10-3).
2) The SELECT statement has the FROM clause mandatory (page 87, Figure 6-1).
So, in this case, to insert multiple rows using just one INSERT statement we need an auxiliary table with just one row:
CREATE TABLE dual(value INT PRIMARY KEY CHECK(value = 1))
INSERT dual(value) VALUES(1)
and then
INSERT INTO table-name (columns) -- the columns are not mandatory
SELECT values FROM dual
UNION ALL
SELECT another-values FROM dual
UNION ALL
SELECT another-values FROM dual
Edit 2: For SQL Server 2008+
Starting with SQL Server 2008 we can use row constructors: (values for row 1), (values for row 2), (values for row 3), etc. (page 218).
So,
INSERT INTO TestInsert
VALUES (1,'a'), --The string delimiter is ' not ‘...’
(2,'b'),
(3,'c'),
(4,'d'),
(5,'e')
will work on SQL Server 2008+.

SQL Server Compact. There was an error parsing the query

I cannot figure out why this is not working. I get the same thing when I try to do an update query as well.
Here is the error " There was an error parsing the query. [ Token line number = 1,Token line offset = 43,Token in error = where ] "
Here is the actual Query INSERT INTO ads (title,price,body,enabled,where,interval,posted) VALUES('test','899','test',True,'Columbus',15,'11/25/2009 10:12:30 AM')
Where would be 'Columbus'
I am using visual studio express 2008 C#
WHERE is a reserved word, try wrapping it in brackets
INSERT INTO ads (title,price,body,enabled,[where],interval,posted)
VALUES('test','899','test',True,'Columbus',15,'11/25/2009 10:12:30 AM')
i think you should provide the value of the primary key in your insert statement,maybe SQL Server Compact databases are not generated automatically or you dont configure that.
I had the same problem this is the INSERT statement which was not working and got the same error:
INSERT INTO Customers(CustomerName,CustomerAddress,CustomerPhone)
VALUES ('Osama','Amman','656565')
this is the INSERT statement which was working fine:
INSERT INTO Customers(CustomerID,CustomerName,CustomerAddress,CustomerPhone)
VALUES ('4564','Osama','Amman','656565')
also if you have in your table columns with names have spaces like (Customer Name)
you must use brackets in your sqlCe statement as:
INSERT INTO Customers([CustomerID],[Customer Name],[Customer Address],[Customer Phone])
VALUES ('4564','Osama','Amman','656565')
also if you use SELECT SCOPE_IDENTITY() to get last record inserted in INSERT Statement
as:
INSERT INTO Customers(CustomerID,CustomerName,CustomerAddress,CustomerPhone)
VALUES ('4564','Osama','Amman','656565') SELECT SCOPE_IDENTITY()
don't use it...

SQL Command to execute multiple times?

I have situations that I need to write multiple rows of the same value to setup some tables. Say I have to add 120 rows with two columns populated. I am looking for a shortcut, instead of having the Insert line repeated n times. How to do this?
In SQL Server Management Studio, you can use the "GO" keyword with a parameter:
INSERT INTO YourTable(col1, col2, ...., colN)
VALUES(1, 'test', ....., 25)
GO 120
But that works only in Mgmt Studio (it's not a proper T-SQL command - it's a Mgmt Studio command word).
Marc
How about
Insert Table( colsnames )
Select Top 120 #value1, #Value2, etc.
From AnyTableWithMoreThan120Rows
Just make sure the types of the values in the #Value list matches the colNames List
what about
insert into tbl1
(col1,col2)
(select top 120 #value1,#value2 from tbl2)
if in sql server 2008 . new in sql server 2008 to insert into a table multiple rows in a single query .
insert into tbl1
(col1,col2)
values
(#value1,#value2),(#value1,#value2),.....(#value1,#value2)
Put the values in an unused table for safe keeping. From there you can insert from this table to the tables you need to setup.
Create an Excel Spreadsheet with your data.
Import the speadsheet into Sql Server.
You can even try with something like this(just an example)
declare #tbl table(col1 varchar(20),col2 varchar(20))
; with generateRows_cte as
(
select
1 as MyRows
union all
select
MyRows+1
from generateRows_cte
where MyRows < 120
)
insert into #tbl(col1,col2)
select
'col1' + CAST(MyRows as varchar),'col2' + CAST(MyRows as varchar)
from generateRows_cte OPTION (MAXRECURSION 0)
select * from #tbl
Note:- Why not you are trying with Bulk insert into SqlServer from a dataset ? I didnot notice first that u have a front end too(VB)!