Cannot set IDENTITY_INSERT in batch - sql

Case
Currently I'm working on database seeding script, executed with sqlcmd. For example this script sample:
IF (SELECT COUNT(*) FROM Genders)=0
BEGIN
PRINT N'Seeding table Genders...'
SET IDENTITY_INSERT Genders ON
GO
INSERT INTO Genders (GenderId, Description) VALUES (0, 'Onbekend');
INSERT INTO Genders (GenderId, Description) VALUES (1, 'Vrouw');
INSERT INTO Genders (GenderId, Description) VALUES (2, 'Man');
INSERT INTO Genders (GenderId, Description) VALUES (3, 'Onzijdig');
INSERT INTO Genders (GenderId, Description) VALUES (4, 'Vrouwman');
INSERT INTO Genders (GenderId, Description) VALUES (5, 'Manvrouw');
SET IDENTITY_INSERT Genders OFF
END
GO
Problem
However, if I execute it with sqlcmd mode in SQL Server Management Studio, it gives me this errors:
Msg 102, Level 15, State 1, Line 5 Incorrect syntax near 'ON'.
Msg 102, Level 15, State 1, Line 10 Incorrect syntax near 'END'.
Googled some, but can't figure out what I'm doing wrong. Without the IF/BEGIN/END if does work, but I like to perform the check first.
Questions:
Anything I'm doing wrong?
If impossible, any workaround available?
Thanks in advance!!

You cannot have GO within BEGIN and END. Try following
SET IDENTITY_INSERT Genders ON
IF (SELECT COUNT(*) FROM Genders)=0
BEGIN
PRINT N'Seeding table Genders...'
INSERT INTO Genders ...
END
SET IDENTITY_INSERT Genders OFF
GO

Related

MariaDb Begin End in a multi-value Insert

Im using MariaDb and Im trying to execute a multi-value insert query using Begin End and I get the following error:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'END' at line 1 0.000 sec
BEGIN;
INSERT INTO Client (Id, Name, Total)
VALUES
(1, 'John', 1000),
(2, 'Mary', 3000),
(3, 'Mike', 14000),
(4, 'George', 15000);
END;
Can I use Begin, End with a multi-value insert and if yes what is the correct syntax here?
I have millions of data to insert. the above is just a small sample of my dataset

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

SQL Server : newline character

I have a programming language that does not allow me to write queries on multiple lines. It has to be written all in a single line.
I am unable to send a GO command because it has to be in a new line..
So for example, this does not work:
insert into mytable (field1, filed2) values (1, 2), (3, 4); go
as it should be
insert into mytable (field1, filed2) values (1, 2), (3, 4);
go
I've tried multiple things but none worked:
insert into mytable (field1, filed2) values (1,2),(3,4); \r go
insert into mytable (field1, filed2) values (1,2),(3,4); \r\n go
insert into mytable (field1, filed2) values (1,2),(3,4); $r$n go
insert into mytable (field1, filed2) values (1,2),(3,4); char(10) go
insert into mytable (field1, filed2) values (1,2),(3,4); char(13) go
Is there a way to write it inline, and have SQL Server use it as 2 different lines?
GO is not part of the TSQL language. It is used and recognized only by SSMS and sqlcmd to cut your script into parts ("batches"), and then each part is compiled and run separately, one after the other.
The reason that SSMS and sqlcmd work this way this is that it makes it possible to have e.g. a CREATE TABLE statement, followed by INSERT statements for that table. The INSERT-part will only compile if the table already exists, and that will be the case only after the CREATE has been run.
It is OK to combine multiple INSERTs into one statement. When in doubt about where the next statement should start, you can add a semicolon (;) to mark the end of the previous statement.

EXPLAIN Failed. 3706: (-3706)Syntax error: expected something between ')' and ','

I've seen this error mentioned on StackOverflow, but not in the context I am using. I am relatively new to Teradata and this behavior is throwing me for a loop. Here is code that works:
INSERT INTO test_table (this, that) VALUES (1, 2);
Here is code that throws the error:
INSERT INTO test_table (this, that) VALUES (1, 2), (3, 4);
This is super confusing to me because the Teradata docs have the following example:
INSERT INTO cities VALUES (2, 'San Jose'), (3, 'Oakland');
Could someone show me what am I missing here? Thanks!
Teradata only allows you to insert one record with a single values. You can see this in the syntax diagram for insert -- there is no "backloop".
Two inserts is a simple workaround:
INSERT INTO test_table (this, that)
VALUES (1, 2);
INSERT INTO test_table (this, that)
VALUES (3, 4);

Inserting data using SQL Server Management Studio

I am trying to use the following syntax to insert information into a database:
INSERT [dbo].[Catalog] ([CatalogID], [CatalogName], [CatalogLastUpdated], [CatalogMediaType], [CatalogComment])
VALUES (0, N'Important Info', CAST(0x00009F9700FC30F0 AS DateTime), N'Folder', N'lsafjalsdkf')
But I get the following error:
Msg 544, Level 16, State 1, Line 1
Cannot insert explicit value for identity column in table 'Catalog' when IDENTITY_INSERT is set to OFF.
I'm not sure what I'm doing wrong,..
Don't specify a value for the IDs. Let the database generate values for you:
INSERT [dbo].[Catalog] ([CatalogName], [CatalogLastUpdated], [CatalogMediaType], [CatalogComment]) VALUES (N'Important Info', CAST(0x00009F9700FC30F0 AS DateTime), N'Folder', N'lsafjalsdkf')
Alternatively you can enable identity insert:
SET IDENTITY_INSERT [dbo].[Catalog] ON
-- your INSERT statements here
SET IDENTITY_INSERT [dbo].[Catalog] OFF