Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Bulk insert in procedure. Use of quotes for variables


Bulk insert in procedure. Use of quotes for variables

Author
Message
ohpenot
ohpenot
Grasshopper
Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)

Group: General Forum Members
Points: 20 Visits: 106
Hi everybody,

I struggled with a bulk insert in procedure and i eventually found a working piece of code. Except that i did'nt understand the general rules to wrap variables.

Exemple :
N'BULK INSERT ' + QUOTENAME(@nomtable) + N'FROM ''' +
@chemin + @filename +
N''' WITH (FIELDTERMINATOR = '';'', ROWTERMINATOR = '''', FIRE_TRIGGERS)'

This is working, but why are we using ''' ??
Now something that's not working..

SET @effacer = 'master.dbo.xp_cmdshell ''DEL '+ @chemin + @filename
EXECUTE (@effacer)

Something wrong with quotes i guess....

I'd like to understand the rule for this!
ohpenot
ohpenot
Grasshopper
Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)

Group: General Forum Members
Points: 20 Visits: 106
I t works using ''''

SET @effacer = 'master.dbo.xp_cmdshell ''DEL '+ @chemin + @filename +''''

but i don't understand why (tried everything)
j.miner
j.miner
SSC Journeyman
SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)

Group: General Forum Members
Points: 88 Visits: 358
It all has to do about strings ... And what is truely a string?

First example does not have quotes in the string.

-- Simple string
DECLARE @X VARCHAR(128);
SET @X = 'A SIMPLE STRING';
PRINT @X;


Second example uses extra quote to denote usage as a literal, not a start of a string.

-- Escaping the quotes
DECLARE @Y VARCHAR(128);
SET @Y = 'A STRING WITH ''' + CONVERT(CHAR(10), GETDATE(), 101) + ''' QUOTES AROUND THE DATE';
PRINT @Y;


Third example uses the ANSI character for quote.

-- Using CHAR()
DECLARE @Z VARCHAR(128);
SET @Z = 'A STRING WITH ' + CHAR(39) + CONVERT(CHAR(10), GETDATE(), 101) + CHAR(39) + ' QUOTES AROUND THE DATE';
PRINT @Z;


You can use the system function QUOTENAME() to make quoted strings.

http://msdn.microsoft.com/en-us/library/ms176114.aspx


You can even change the character used for escaping (ESCAPE) in a LIKE statement.

http://msdn.microsoft.com/en-us/library/ms179859.aspx


I hope this helps.

Sincerely

John

John Miner
Crafty DBA
www.craftydba.com
ohpenot
ohpenot
Grasshopper
Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)

Group: General Forum Members
Points: 20 Visits: 106
hi,
yes it helps a lot !

thank you very much. I ll try this tomorrow at work
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search