|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Tuesday, June 05, 2012 12:03 PM
Points: 117,
Visits: 163
|
|
Or you could use a substatute char in the string. There are obvious disadvantages with this, but there is a lot less string concantination and that is a good thing...
DECLARE @sql nvarchar(2000), @pram1 nvarchar(100) SET @pram1 = 'West'
SET @sql = Replace( ' SELECT col1 AS "Hello Kitty", col2 AS "Today is Today", col3 AS "War is hell" FROM dbo.table1 WHERE col4 = "' + @pram1 + '"' , '"', '''')
PRINT @sql
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Friday, May 03, 2013 11:59 AM
Points: 16,
Visits: 85
|
|
| You can write a UDF that receives a string and returns the string enclosed in single quotes. In your SQL code, you have a re-usable function that you can use throughout your database.
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, January 26, 2006 8:41 AM
Points: 3,
Visits: 1
|
|
i wrote a function a while back for this same problem CREATE FUNCTION dbo.Quote() RETURNS CHAR(1) AS BEGIN RETURN '''' END beauty of the function is it's always available (without the DECLARE and SET statements), and it can be used in views (inline) ejf
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Sunday, May 22, 2011 6:15 PM
Points: 65,
Visits: 80
|
|
One of those - "Doh, Why didn't I think of that?" Simple and smart - well done.
Best Regards Terry
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Sunday, September 16, 2012 3:26 AM
Points: 1,038,
Visits: 443
|
|
Yes quotename is a nice way of doing things, but bear in mind it has a limit of nvarchar(258) so you probably are better off using a UDF so you don't get caught out working with a really really big string  Nice method using the variable though, although perhaps a little @q is less intrusive? Using a substitute char also makes good sense, like a ~ character or something not often used...
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Saturday, November 19, 2005 2:36 AM
Points: 1,
Visits: 1
|
|
Mr.Allen, you provided a very good another logic to get away from confusing code. I understood how to use table data type practically and thanks for the author and Mr.Allen too for a nice example of using qutoname function.
|
|
|
|