Sunday, March 11, 2012

Double Quotes and Print

Hi:
This is a very simple thing but some how i have a mental block. what I am
trying to
do is concatenate a string that gets passed with double quotes to a single
quoted string and trying to print but I get an error. here is what I am
trying to do:
DECLARE @.drive varchar(255)
DECLARE @.cmd varchar(200)
SET @.drive="c:\bak"
SET @.cmd='md ' + @.drive
PRINT (@.cmd)
I get an error when i execute the above.
what i am looking is to print the following when i execute the above code.
so basically i want md printed with the drive name and the folder name in
double quotes besides it.
md "c:\bak"
Can anyone please fix what I am doing wrong. looks like i am missing few
quotes.
Thanks
M> SET @.cmd='md ' + @.drive
Enclose this string in single-quotes:
SET @.drive='"c:\bak"'
Hope this helps.
Dan Guzman
SQL Server MVP
"Meher" <Meher@.discussions.microsoft.com> wrote in message
news:93C59AF3-9C22-483E-BD96-9EF5BD3C1825@.microsoft.com...
> Hi:
> This is a very simple thing but some how i have a mental block. what I am
> trying to
> do is concatenate a string that gets passed with double quotes to a single
> quoted string and trying to print but I get an error. here is what I am
> trying to do:
> DECLARE @.drive varchar(255)
> DECLARE @.cmd varchar(200)
> SET @.drive="c:\bak"
> SET @.cmd='md ' + @.drive
> PRINT (@.cmd)
> I get an error when i execute the above.
> what i am looking is to print the following when i execute the above code.
> so basically i want md printed with the drive name and the folder name in
> double quotes besides it.
> md "c:\bak"
> Can anyone please fix what I am doing wrong. looks like i am missing few
> quotes.
> Thanks
> M
>
>|||Hi Dan:
Thanks for the reply. I tried that already before but my problem is the
drive name is passed as a parameter @.drive name from the stored procedure an
d
so when I concatenate the variable with single quotes i get an error.
something i did like this
DECLARE @.drive varchar(255)
DECLARE @.cmd varchar(200)
SET @.drive="c:\bak"
SET @.drive=''+@.drive+''
SET @.cmd='md ' + @.drive
PRINT (@.cmd)
Which still throws an error. The issue here is the drive name is passed as a
paramater to the sproc with double quotes like
Exec testprocedure @.drive="C:\bak"
So i would either need to replace the quotes i guess.
Any suggestions?
Thanks
"Dan Guzman" wrote:

> Enclose this string in single-quotes:
> SET @.drive='"c:\bak"'
> --
> Hope this helps.
> Dan Guzman
> SQL Server MVP
> "Meher" <Meher@.discussions.microsoft.com> wrote in message
> news:93C59AF3-9C22-483E-BD96-9EF5BD3C1825@.microsoft.com...
>
>|||Hi,
How's this?
DECLARE @.drive varchar(255)
DECLARE @.cmd varchar(200)
SET @.drive='c:\bak'
SET @.cmd='md "' + @.drive + '"'
PRINT (@.cmd)
Robert
"Meher" <Meher@.discussions.microsoft.com> wrote in message
news:93C59AF3-9C22-483E-BD96-9EF5BD3C1825@.microsoft.com...
> Hi:
> This is a very simple thing but some how i have a mental block. what I am
> trying to
> do is concatenate a string that gets passed with double quotes to a single
> quoted string and trying to print but I get an error. here is what I am
> trying to do:
> DECLARE @.drive varchar(255)
> DECLARE @.cmd varchar(200)
> SET @.drive="c:\bak"
> SET @.cmd='md ' + @.drive
> PRINT (@.cmd)
> I get an error when i execute the above.
> what i am looking is to print the following when i execute the above code.
> so basically i want md printed with the drive name and the folder name in
> double quotes besides it.
> md "c:\bak"
> Can anyone please fix what I am doing wrong. looks like i am missing few
> quotes.
> Thanks
> M
>
>|||Hi Robert:
The variable is passed in double quotes to the sproc and so i can have
single quotes around the c:\bak. Anyway I figured it out. I turn off the
Quoted_Identifier off and turn it back on and then perform the concatenation
.
Here is what I do:
SET QUOTED_IDENTIFIER OFF
DECLARE @.drive varchar(255)
DECLARE @.cmd varchar(200)
SET @.drive="c:\bak"
SET @.drive=""""+@.drive+""""
SET QUOTED_IDENTIFIER ON
SET @.cmd='md ' + @.drive
PRINT (@.cmd)
"Robert Ellis" wrote:

> Hi,
> How's this?
> DECLARE @.drive varchar(255)
> DECLARE @.cmd varchar(200)
> SET @.drive='c:\bak'
> SET @.cmd='md "' + @.drive + '"'
> PRINT (@.cmd)
> Robert
>
> "Meher" <Meher@.discussions.microsoft.com> wrote in message
> news:93C59AF3-9C22-483E-BD96-9EF5BD3C1825@.microsoft.com...
>
>|||> Exec testprocedure @.drive="C:\bak"
> So i would either need to replace the quotes i guess.
No need to guess. There will be no quotes around the @.drive value with the
code you posted. The quotes (double or single) around the procedure
parameter value are used only as string enclosures and will not be included
in the value passed to the proc. All you need to do is add double-quotes
around the @.drive value:
ALTER PROC testprocedure
@.drive varchar(255)
AS
DECLARE @.cmd varchar(200)
SET @.drive='"'+@.drive+'"'
SET @.cmd='md ' + @.drive
PRINT (@.cmd)
GO
EXEC testprocedure @.drive="C:\bak"
GO
By the way, it's a good practice to use only single-quotes to encclose
string literals. You'll get the same result with:
EXEC testprocedure @.drive='C:\bak'
Hope this helps.
Dan Guzman
SQL Server MVP
"Meher" <Meher@.discussions.microsoft.com> wrote in message
news:4B8493D1-412F-4DA9-B1C1-BCCE0A8C126C@.microsoft.com...
> Hi Dan:
> Thanks for the reply. I tried that already before but my problem is the
> drive name is passed as a parameter @.drive name from the stored procedure
> and
> so when I concatenate the variable with single quotes i get an error.
> something i did like this
> DECLARE @.drive varchar(255)
> DECLARE @.cmd varchar(200)
> SET @.drive="c:\bak"
> SET @.drive=''+@.drive+''
> SET @.cmd='md ' + @.drive
> PRINT (@.cmd)
> Which still throws an error. The issue here is the drive name is passed as
> a
> paramater to the sproc with double quotes like
> Exec testprocedure @.drive="C:\bak"
> So i would either need to replace the quotes i guess.
> Any suggestions?
> Thanks
> "Dan Guzman" wrote:
>

No comments:

Post a Comment