Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Double Quotes

Author  Topic 

mapidea
Posting Yak Master

124 Posts

Posted - 2010-02-23 : 11:47:28
DECLARE @Sql_Statement VARCHAR(1000)

SET @Sql_Statement = 'SELECT
o.code as custnum
FROM Orders o
WHERE order_id IN (' + @order_ids + ')'

EXEC @Sql_Statement


I want the result to be with double quotes. How can I change the dynamic Sql.

"custnum1"
"custnum2"
"custnum3"
"custnum4"

Sai

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2010-02-23 : 11:51:04
SET @Sql_Statement = 'SELECT ''"'' + o.code + ''"'' as custnum
FROM Orders o
WHERE order_id IN ('' + @order_ids + '')'''


=======================================
Few things are harder to put up with than the annoyance of a good example. (Mark Twain)
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-02-23 : 11:55:09
Why use dynamic SQL? There is risk of SQL Injection doing this - plus the user will need permissions on the underlying table - rather than just the Stored Procedure
Go to Top of Page

mapidea
Posting Yak Master

124 Posts

Posted - 2010-02-23 : 12:05:04
Your code gives the message

The multi-part identifier "o.code" could not be bound.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-23 : 12:07:14
quote:
Originally posted by mapidea

DECLARE @Sql_Statement VARCHAR(1000)

SET @Sql_Statement = 'SELECT
o.code as custnum
FROM Orders o
WHERE order_id IN (' + @order_ids + ')'

EXEC @Sql_Statement


I want the result to be with double quotes. How can I change the dynamic Sql.

"custnum1"
"custnum2"
"custnum3"
"custnum4"

Sai


To be honest you dont need to use D-SQL here. you can just use like

SELECT
o.code as custnum
FROM Orders o
WHERE ',' + @order_ids + ',' LIKE '%,' + CAST(order_id AS varchar(15)) + ',%'

provided you pass a comma separated list as parameter value

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

mapidea
Posting Yak Master

124 Posts

Posted - 2010-02-23 : 12:10:00
Thanks a lot Visakh
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-23 : 12:21:01
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -