I am using Microsoft SQL Server Management Studio, I am trying to run the following query to input values into a temporary table to use later:
CREATE TABLE #temptable
(colnumber varchar(15), dispcode varchar(10))
INSERT INTO #temptable (colnumber, dispcode)
VALUES
('col5', '811'),
('col6', '817'),
('col7', '823'),
('col8', '825');
When running I get the following error:
Msg 102, Level 15, State 1, Line 50
Incorrect syntax near ','.
Which points to the line "('col5', '811'),"
Could anyone help me identify the problem here?
For SQL Server version <2008 use this:
INSERT INTO #temptable (colnumber, dispcode)
SELECT 'col5', '811'
UNION ALL SELECT 'col6', '817'
UNION ALL SELECT 'col7', '823'
UNION ALL SELECT 'col8', '825'
Related
I am trying to find if there is any way, selecting table from another pc, 'without' creating a linked server.
I found an example which i setting up my sql connection string inside my query, but it gives me error that i need to create a linked server.
SELECT * FROM OPENDATASOURCE (
'SQLNCLI'
,'Data Source=192.168.0.3,1433;Initial Catalog=wiorder;User ID=admin;Password=1234').inventorymaster
Solved by Dan.
When i am trying to insert from destination pc to local pc i am getting error:
Msg 8114, Level 16, State 5, Line 4
Msg 8101, Level 16, State 1, Line 3
An explicit value for the identity column in table 'InventoryMaster' can only be specified when a column list is used and IDENTITY_INSERT is ON.
Here is my query
SET IDENTITY_INSERT InventoryMaster ON
insert into InventoryMaster
SELECT * FROM OPENDATASOURCE (
'SQLNCLI'
,'Data Source=192.168.0.3,1433;Initial Catalog=WiOrder;User ID=admin;Password=1234').WiOrder.dbo.inventorymaster
SET IDENTITY_INSERT InventoryMaster OFF
I am trying to use a query modified from MS docs to insert GEOMETRY data from my sql server into a linked sql server (Azure).
openquery for select works fine with the client's choice of naming for their db which includes '-' ([their-db-name]).
SELECT * FROM openquery([LINKED SERVER],
'SELECT [geometry] FROM [their-db-name].[theirSchema].[Dimensions]');
** correct result set returned **
However when I try INSERT I get:
INSERT openquery([LINKED SERVER],
'SELECT [geometry] FROM [their-db-name].[theirSchema].[Dimensions]')
SELECT [geometry] FROM [my_db].[dbo].[Dimensions];
Msg 102, Level 15, State 1, Line 27
Incorrect syntax near '-'.
I don't have any control over their use of '-' in naming, however since the name is enclosed in '[]', and the openquery SELECT statement works fine I have exhausted all the syntax errors I can think of.
Using 'INSERT INTO' makes no difference.
How can I make an insert work?
Thanks in advance!
This is probably something really trivial I'm missing, but I can't seem to figure out why it's not working:
Basically, this works:
DECLARE #names TABLE (name NVARCHAR(100));
INSERT INTO #names
VALUES ('John');
but this does not:
DECLARE #names TABLE (name NVARCHAR(100));
INSERT INTO #names
VALUES ('John'), ('Jane');
I'm getting this error:
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near ','.
Why wouldn't this work? I've done this thousands of times with SSMS 2008.
SQL Server Table Value Constructor (Transact-SQL) was introduces in SQL Server 2008.
SQL Server 2008 and Later
DECLARE #names TABLE (name NVARCHAR(100));
INSERT INTO #names
VALUES
('John'),
('Jane');
SQL Server 2000 and Later
Any older version you will need to use single row insert at a time
DECLARE #names TABLE (name NVARCHAR(100));
INSERT INTO #names VALUES('John');
INSERT INTO #names VALUES('Jane');
It's not a matter of what SSMS supports -- SSMS is just sending the query you've entered to SQL Server and letting SQL Server decide whether the syntax is valid. The issue is that not every version (e.g. SQL Server 2005) of SQL Server supports this comma-delimited syntax for insert statements.
I am wondering why below statement is not working:
insert into #temp (ip, ping) values ('ip', exec xp_cmdshell 'ping ip')
I would like to get resultset where I will have ip address in one column and ping from that server. Above query returns errors:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'exec'.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ')'.
Thanks in advance for explanations.
You can't execute anything in a INSERT statement value list. You need to execute like this:
insert into #temp
execute xp_cmdshell 'ping ip'
[This assumes that the #temp table has a column structure matching the resultset of xp_cmdshell ]
You can also create an intermediate temp table to contain all the columns that the stored procedure returns and then insert just the ones you want into your final table.
Well, insert ... exec is notoriously inflexible. For one thing, it won't work with a values clause. Even more annoyingly, it does not support a column list. The columns in the table have to match the output of the stored procedure exactly.
The only way to use insert ... exec is:
insert TableName exec(...)
-- ^^^--> no column list!
Here's an example:
if exists (select * from tempdb.sys.tables where name like '#temp__%')
drop table #temp
create table #temp (output varchar(max))
insert into #temp execute xp_cmdshell 'ping pong'
select * from #temp
I have two tables CrossDBTrigTest_1 and CrossDBTrigTest_2 on same SQL Server instance.
The databases both have a table called Employee.
I wrote the following trigger on the Employee table CrossDBTrigTest_1 db:
Create Trigger [dbo].[CrossDBInsert] on [dbo].[employee] after insert
AS
Begin
Set nocount on
Insert into CrossDBTrigTest_2.employee(FirstName, LastName, Date)
SELECT inserted.FirstName, inserted.LastName, getdate()
FROM inserted
End
but the Insert statement fails with message:
Msg 208, Level 16, State 1, Procedure CrossDBInsert, Line 5
Invalid object name 'CrossDBTrigTest_2.employee'.
How do I enable cross database triggers in situations like this??
Shouldn't
CrossDBTrigTest_2.employee(FirstName,LastName,Date)
be
CrossDBTrigTest_2.dbo.employee(FirstName,LastName,Date)
???
Use
CrossDBTrigTest_2..employee
as table name. Note two dots instead of one.