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.
Related
I created a Stored procedure in SQL Server Standard Edition which works fine but when I am trying to apply same procedure in SQL Server Express edition but I am getting following error
Incorrect syntax near the keyword 'with'. If this statement is a
common table expression, an xmlnamespaces clause or a change tracking
context clause, the previous statement must be terminated with a
semicolon.
Below is the piece of code where the error is coming
DECLARE #ProcessId AS INT
CREATE TABLE #Temp_ReadJson (
BucketId INT, StatusType VARCHAR(50)
);
DECLARE #json NVARCHAR(max)=(SELECT TOP 1 ProcessJSON FROM MASTER.Process WHERE ID=#ProcessId)
INSERT INTO #Temp_ReadJson
SELECT BucketId, StatusType FROM OPENJSON(#json, '$.Connections')
WITH (BucketId INT, StatusType VARCHAR(50))--Error is coming in this line
what I need to do here?
OPENJSON was introduced in SQL Server 2016 so your database must be in compatibility level 130 or greater in order to use the function. Since this is apparently a development database, make sure your database has the same compatibility level as production. This can be changed ALTER DATABASE like the example below.
ALTER DATABASE YourDatabase SET COMPATIBILITY_LEVEL = 150;
add a semicolon before with ..
SELECT BucketId, StatusType FROM OPENJSON(#json, '$.Connections');
WITH (BucketId INT, StatusType VARCHAR(50))
Hi I created a stored procedure that uses OPEN JSON and insert data into a table.
The problem is when I run the stored procedure it shows an error.
I am using SQL server 2016 (SQl Server 13.0.4446.0). I am not getting the same issue when using using sql server 13.0.1742.0
CREATE PROCEDURE [dbo].Test2--'[{"FileId":1,"DataRow":"3000926900"}]'
(
#data varchar(max)
)
AS
BEGIN
create table #Temp
(
FileId bigint,
DataRow nvarchar(max),
DateLoaded DateTime
)
INSERT INTO [dbo].#Temp
SELECT * FROM OPENJSON(#data)
WITH (FileId bigint,
DataRow nvarchar(max),
DateLoaded DateTime)
select * from #temp
END
Error:
If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
Check your database compatibility level. OPENJSON is new with SQL Server 2016, and if your compatibility level is set "SQL Server 2014 (120)" or less the OPENJSON function will not be correctly recognized or executed. See the MSDN docs at https://learn.microsoft.com/en-us/sql/t-sql/functions/openjson-transact-sql .
Scenario: get trigger-generated primary key when calling INSERT INTO from SQL Server linked server to Oracle
Given
Oracle 11g table with columns PRIMARY_KEY_ID, FIELD1, FIELD2, CREATE_DATE. Table has "BEFORE INSERT" trigger that selects NEXTVAL from a sequence into PRIMARY_KEY_ID field.
SQL Server 2008 R2 with Linked Server to the Oracle database containing the table above.
When I insert a record into the Oracle table, then I want to retrieve the trigger-generated primary key.
How do I do this?
Make sure these properties are set on the SQL Server linked server:
RPC=True
RPC Out=True
Execute this code in SQL Server:
DECLARE #Field1 NVARCHAR(42);
DECLARE #Field2 NVARCHAR(42);
DECLARE #PrimaryKeyValue INT;
EXECUTE (
'begin INSERT INTO MYSCHEMA.MYTABLE (
FIELD1
,FIELD2
,CREATE_DATE
)
VALUES (
?
,?
,sysdate
) RETURNING PRIMARY_KEY_ID INTO :PrimaryKeyValue; end;'
,#Field1
,#Field2
,#PrimaryKeyValue OUTPUT
) at oracle_linked_server;
Notes
begin and end; are required in the statement.
The #PrimaryKeyValue variable declared in SQL Server is the same as the :PrimaryKeyValue output parameter; Oracle uses a colon prefix for parameters.
See Calling Oracle stored procedure with output parameter from SQL Server, which provided the inspiration for this answer.
I'm trying to insert a row with TIMESTAMP(6) column in an Oracle 11g table from SQL Server 2008 script, through Linked Server.
This is what I tried so far:
INSERT INTO LinkedServer..Schema.Table(TimeStampColumn)
VALUES(CONVERT(DATE, '2013-08-07'));
INSERT INTO LinkedServer..Schema.Table(TimeStampColumn)
VALUES(CONVERT(DATETIME, '2013-08-07 12:12:12.000001'));
INSERT INTO LinkedServer..Schema.Table(TimeStampColumn)
VALUES(CONVERT(TIMESTAMP, '2013-08-07 12:12:12.000001'));
and many combinations, every time I get this error:
The OLE DB provider "OraOLEDB.Oracle" for linked server "LinkedServer" supplied invalid metadata for column "TimeStampColumn". The data type is not supported.
Is this possible?
How can I convert SQL Server's varchar or datetime value to Oracle timestamp(6) data type?
Thanks a lot!
well, i have found it:
EXECUTE ('begin INSERT INTO TEST_TIMESTAMP(TimeStampColumn)
VALUES (TO_TIMESTAMP(?,''YYYY-MM-DD HH24:MI:SS.FF6'')); end;',
'2013-12-06 11:12:13.123456')
AT LINKEDSERVER;
'timestampcolumn' is column with type TIMESTAMP(6)
the same way you can use to call oracle functions:Calling an Oracle function from SQL Server Linked Server
and it also works with variable
declare #date datetime2
set #date = SYSDATETIME()
EXECUTE ('begin INSERT INTO TEST_TIMESTAMP(TimeStampColumn)
VALUES (?); end;',
#date)
AT LINKEDSERVER;
BUT in this case Oracle truncates it to seconds
I cannot get this to work. I have opened a SQL Server Express table in SQL Server Management Studio 2005. When I try to paste a multiline text snippet into an NTEXT field it gets truncated to only include the first line.
In Access these kind of things works, what should i do?
Rewrite it as an UPDATE or INSERT INTO statement. String literals can span multiple lines:
declare #t table (d varchar(10))
insert into #t values ('a
b
c')
We have had the same issue.
You can either use and insert statement
INSERT INTO TABLE (Col1,..., Coln)
SELECT Val1,...,
'MULTILINE
VALUE',...,Valn
Or use Access with linked tables to insert the values with multi lines.