Placing a variable inside single quotes - variables

I have a simple data import script I'm running in MSSQL. If I statically assign the data filename to be imported, it works great.
BULK
INSERT TRACKED_IMPORT
FROM 'C:\SQLIMPORT\DATAFILE.CSV'
WITH (FIRSTROW = 2, FIELDTERMINATOR = ';', ROWTERMINATOR = '0x0a')
However, I'd like to use a variable instead of a static filename. Something like this:
DECLARE #IMPORTFILENAME VARCHAR(30) = 'C:\SQLIMPORT\DATAFILE.CSV';
BULK
INSERT TRACKED_IMPORT
FROM #IMPORTFILENAME
WITH (FIRSTROW = 2, FIELDTERMINATOR = ';', ROWTERMINATOR = '0x0a')
When I do that, I get the following errors:
Msg 102, Level 15, State 1, Line 23
Incorrect syntax near '#IMPORTFILENAME'.
Msg 319, Level 15, State 1, Line 24
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.
Is there a way to allow for a variable filename?

Related

How can i get rid of this error: Msg 102, Level 15, State 1, Procedure assignhousekeeping, Line 13 [Batch Start Line 57] Incorrect syntax near ')' [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am writing a trigger code to insert a data to cleaning table when a new data is inserted in condos table. this is the code that I have written:
create trigger assignhousekeeping
on condos
after insert
as
insert into CLEANING(Condo#, DateCleaned, HKID)
value(
(select Condo# from inserted),
getdate(),
(select PID
from PERSONNEL
where FName = 'janice'
and LName = 'avery'));
After running this I get these error messages:
Msg 102, Level 15, State 1, Procedure assignhousekeeping, > Line 6 [Batch Start Line 57]
Incorrect syntax near 'value'.
Msg 102, Level 15, State 1, Procedure assignhousekeeping, > Line 8 [Batch Start Line 57]
Incorrect syntax near ','.
Msg 102, Level 15, State 1, Procedure assignhousekeeping, > Line 13 [Batch Start Line 57]
Incorrect syntax near ')'.
and the trigger did not go off as well. How can I fix this?
The correct insert syntax uses values not value. However, that is only the beginning of your problems, because the trigger assumes that inserted has only one row.
I can speculate that you actually want:
insert into CLEANING (Condo#, DateCleaned, HKID)
select Condo#, getdate(), p.pid
from inserted i left join
PERSONNEL p
on p.FName = 'janice' and LName = 'avery';

How to combine If statement with Substring command in SQL?

I have two types of entries in one column. One type starts with V2, the other starts with a digit. How do I write a query in SQL where I can extract different part of the string based on how it starts?
TextStringColumn
V2|T:GSK|1000000|S1:TES|S2:N/A|Q:24|S:0.5gx3|PD:2020-10-22|C:QQ
2000308|S1:BES|T:SKY|Q:16446G|BSI:BPKGVAXQOHZFWGE
I wrote
SELECT TextStringColumn, If(TextStringColumn like 'V2%',SUBSTRING(TextStringColumn ,10,7),SUBSTRING(TextStringColumn ,1,7)) As NumberCol
FROM TestTable
But I keep getting syntax errors.
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'If'.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ','.
The desired result will be
TextStringColumn NumberCol
V2|T:GSK|1000000|S1:TES|S2:N/A|Q:24|S:0.5gx3|PD:2020-10-22|C:QQ 1000000
2000308|S1:BES|T:SKY|Q:16446G|BSI:BPKGVAXQOHZFWGE 2000308
You may use a CASE expression:
SELECT
CASE WHEN TextStringColumn LIKE 'V2%'
THEN SUBSTRING(TextStringColumn, 10, 7)
ELSE SUBSTRING(TextStringColumn, 1, 7) END AS NumberCol
FROM TestTable;
The above logic assumes the only two varieties of strings are those which start with V2, and those which do not start with V2.
If the strings are variable length ... consider a little JSON
Example
Declare #YourTable Table ([TextStringColumn] varchar(100)) Insert Into #YourTable Values
('V2|T:GSK|1000000|S1:TES|S2:N/A|Q:24|S:0.5gx3|PD:2020-10-22|C:QQ')
,('2000308|S1:BES|T:SKY|Q:16446G|BSI:BPKGVAXQOHZFWGE')
Select A.*
,Val = case when [TextStringColumn] like 'V2%'
then JSON_VALUE(S,'$[2]')
else JSON_VALUE(S,'$[0]') end
From #YourTable A
Cross Apply ( values ( '["'+replace([TextStringColumn],'|','","')+'"]' ) ) B(S)
Returns
TextStringColumn Val
V2|T:GSK|1000000|S1:TES|S2:N/A|Q:24|S:0.5gx3|PD:2020-10-22|C:QQ 1000000
2000308|S1:BES|T:SKY|Q:16446G|BSI:BPKGVAXQOHZFWGE 2000308

Error messages when I execute SQL Query in the Where Statement

I have created a SQL query using SQL Server Management Studio,
But, I faced errors in the Where statement.
Here WHERE clause of the SQL code:
where
(case when 'All' in (select Items from CDB.dbo.Split (#a,','))
then innt.code **IS** NOT NULL
else innt.code in (select Items from CDB.dbo.Split (#a,',')) end) and
I get these error messages:
Msg 156, Level 15, State 1, Line 90
Incorrect syntax near the keyword 'IS'.
Msg 156, Level 15, State 1, Line 92
Incorrect syntax near the keyword 'and'.
Can you guys please help me solve this?
Booleans are not a type in SQL Server. So a case cannot return a boolean expression.
So, just use regular comparisons
where ('All' in (select Items from CDB.dbo.Split(#a, ',') and innt.code IS NOT NULL) or
(innt.code in (select Items from CDB.dbo.Split(#a, ',') )

Bulk insert SQL command cannot insert first row

I'm using a bulk insert command for SQL Server but for some reason the first row isn't being inserted. Why can't I insert data from the first row? Is bulk insert expecting headers as default and how can i circumvent this? If I add a dummy row and set WITH to FIRSTROW = 2 then the first row is inserted without a problem but I don't think this is a nice solution.
Error code:
Msg 4864, Level 16, State 1, Line 1
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 1, column 1 (table_id).
Command:
BULK INSERT TableData
FROM 'C:\Users\Oscar\file.csv'
WITH (FIELDTERMINATOR = ';',
ROWTERMINATOR = '\n',
KEEPNULLS,
KEEPIDENTITY)
Sample data:
1;Text 1;1;0;;
2;Text 2;1;0;;
3;Text 3;1;0;;
4;Text 4;1;0;;
5;Text 5;1;0;;
The script is probably utf-8 and you're trying to load it from cmd with cp-1252 or something, the UTF-Bom at the beginning freaks out the interpreter.
Look with a hexeditor and you'll see it.
Save as ANSI and try again.
I've had problems with Line Feed/Carriage Return Line Feed, this might be the issue here as well. For Line Feed I had to use a Row terminator of 0x0a:
BULK INSERT TableData
FROM 'C:\Users\Oscar\file.csv'
WITH (
FIELDTERMINATOR = ';',
ROWTERMINATOR = '0x0a',
KEEPNULLS,
KEEPIDENTITY)

SQL Server not allowing NULL for Date even though mentioned as null while creation

I am trying to create a table for library management system and i want my date_in to be null for some cases. When I am trying to give null it is giving me error as:
Msg 4864, Level 16, State 1, Line 7
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 8, column 7 (date_in).
Msg 4864, Level 16, State 1, Line 7
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 9, column 7 (date_in).
Msg 4864, Level 16, State 1, Line 7
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 10, column 7 (date_in).
Msg 4864, Level 16, State 1, Line 7
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 11, column 7 (date_in).
Msg 4864, Level 16, State 1, Line 7
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 12, column 7 (date_in).
Msg 4864, Level 16, State 1, Line 7
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 13, column 7 (date_in).
I have explicitly mentioned that attribute to take null but still it is not allowing.
help me
thanks in advance
Here is the query:
create table book_loans
(
loan_id varchar(10) not null,
book_id varchar(10) not null,
branch_id smallint not null,
card_no varchar(10) not null,
date_out date null default null,
due_date date null default null,
date_in date null default null
)
BULK
INSERT book_loans
FROM 'F:\sql project\resources\SQL_library_project_data\book_loans_data.txt'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '<>',
FIRSTROW=2
)
GO
here is the data file:
load_id,book_id,branch_id,card_no,date_out,due_date,date_in<>
1,0399147020,1,9019,2013-11-22,2013-12-06,2013-12-01<>
2,0030059380,4,9007,2013-12-01,2013-12-15,2013-12-16<>
3,0671880756,5,9018,2013-12-08,2013-12-22,2013-12-22<>
4,0911625291,3,9013,2014-01-02,2014-01-16,2014-01-12<>
5,0688161995,5,9022,2014-02-10,2014-02-24,2014-03-01<>
6,0911625291,2,9011,2014-03-03,2014-03-17,2014-03-16<>
7,1861003730,3,9034,2014-04-17,2014-05-01,NULL<>
8,0201612585,3,9034,2014-04-17,2014-05-01,NULL<>
9,1565927699,3,9034,2014-04-17,2014-05-01,NULL<>
10,0192860925,4,9009,2014-04-18,2014-05-02,NULL<>
11,0805057579,1,9021,2014-04-18,2014-05-02,NULL<>
12,0911625607,2,9018,2014-04-19,2014-05-03,NULL
The way BULK INSERT works is that a NULL value needs to be an empty field, i.e. value,2,,3. Two delimeters without a value in between declares null value. In your file, you're using NULL to indicate DB NULL value but there's no way for bulk insert to read that as a DB NULL value because you could be trying to insert string 'NULL' in to the field. You can either remove all NULL s from your text file, or if that's not the option you can bulk import to a temp table where the last column is defined as varchar and after the import if the value of last column is 'NULL' then use a DB null value.
Also see http://msdn.microsoft.com/en-au/library/ms162802.aspx if you want to produce a text file from sql DB.
BULK
INSERT book_loans
FROM 'F:\sql project\resources\SQL_library_project_data\book_loans_data.txt'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '<>',
FIRSTROW=2,
KEEPNULLS
)