I'm running a query using "EXEC sp_helptext Object", but it returns multiple lines with a column name Text. I'm trying to concatenate that value into a single string but I'm having trouble trying to figure out the best way to do it using T-SQL.
You can try something like this
DECLARE #Table TABLE(
Val VARCHAR(MAX)
)
INSERT INTO #Table EXEC sp_helptext 'sp_configure'
DECLARE #Val VARCHAR(MAX)
SELECT #Val = COALESCE(#Val + ' ' + Val, Val)
FROM #Table
SELECT #Val
This will bring back everything in one line, so you might want to use line breaks instead.
Assuming SQL Server 2005 and above (which is implied by varchar(max) in astander's answer), why not simply use one of these
SELECT OBJECT_DEFINITION('MyObject')
SELECT definition FROM sys.sql_modules WHERE object_id = OBJECT_ID('MyObject')
This is not very glamorous, but it works ...
DECLARE #Table TABLE(
Val VARCHAR(MAX)
)
INSERT INTO #Table EXEC sp_helptext 'sp_configure'
DECLARE #Val VARCHAR(MAX)
SET #Val = ''
SELECT #Val = #Val + REPLACE(REPLACE(REPLACE(Val, CHAR(10), ''), CHAR(13), ''), CHAR(9), '')
FROM #Table
-- Replaces line breaks and tab keystrokes.
SELECT #Val
Related
I have 'param1, param2, parma3' coming from SSRS to a stored procedure as a varchar parameter: I need to use it in a query's IN clause but then need to change its format like this first:
select *
from table1
where col1 in('param1', 'param2', 'param3')
What is the best way to reformat the parameter without creating functions and parameter tables?
Try this one, Just need to add commas at the beginning and at the end of #params string.
Declare #params varchar(100) Set #params = ',param1,param2,param3,'
Select * from t
where CHARINDEX(','+cast(col1 as varchar(8000))+',', #params) > 0
SQL FIDDLE
you can use split function and use it as in following way
here my split fnSplitString return splitdata
select * from tb1 where id in(select splitdata from dbo.fnSplitString((select col1 from tb12 where id=3),','))
create FUNCTION [dbo].[fnSplitString]
(
#string NVARCHAR(MAX),
#delimiter CHAR(1)
)
RETURNS #output TABLE(splitdata NVARCHAR(MAX)
)
BEGIN
DECLARE #start INT, #end INT
SELECT #start = 1, #end = CHARINDEX(#delimiter, #string)
WHILE #start < LEN(#string) + 1 BEGIN
IF #end = 0
SET #end = LEN(#string) + 1
INSERT INTO #output(splitdata)
VALUES(SUBSTRING(#string, #start, #end - #start))
SET #start = #end + 1
SET #end = CHARINDEX(#delimiter, #string, #start)
END
RETURN
END
If you are using SQL 2016 and above string_split you can use.
-- #param is where you keep your comma separated values example:
declare #param = 'param1,param2,param3'
select * from table1 where col1 in (select TRIM(value) from string_split(#param,',')
More information about string_split check offical documemt
Furthermore, TRIM() is used to trim values from white spaces.
We can use STRING_SPLIT() in SQL SERVER
DECLARE #params varchar(max)= 'param1,param2,param3'
SELECT *
FROM table1
WHERE col1 IN (SELECT value FROM STRING_SPLIT( #params , ','))
"Best way" is arguable, but one classic approach that remains without "creating functions and table parameters" is to simply employ dynamic SQL in the stored procedure:
-- FORNOW: local to act as the SP param and arg
declare #values varchar(100) = 'param1, param2, param3'
-- Add opening and closing single quotes, then quotes around each
-- comma-separated list item.
select #values = '''' + REPLACE(#values, ', ', ''', ''') + ''''
-- FORNOW: for clarity/debugging
print #values
--'param1', 'param2', 'param3'
-- Run the desired query as dynamic SQL.
DECLARE #sql as nvarchar(250);
SET #sql = 'select * from table1 where col1 in (' + #values + ')';
EXEC sp_executesql #sql;
This assumes a couple things, though:
That commas in the list of values are followed by a space. Variations on this solution can address deviations in this respect of course, but it is important to be aware of this assumption.
That the comma-separated values do not themselves have commas in them – unlikely but worth mentioning since whether values will satisfy this constraint sometimes goes unconsidered.
Load the Params into a string and execute as an sql :
declare #param varchar(1000) = 'param1, param2, parma3'
declare #sql varchar(4000)
select #sql =
'select *
from table1
where col1 in(''' + replace(#param,',',''',''') + ''')'
-- print #sql -- to see what you're going to execute
exec sp_executesql #sql
DECLARE #params varchar(max) = '1,2,3,4'
SELECT * FROM table2 WHERE colId IN (SELECT value FROM SPLIT(#params,','))
Base on id we can find.
I am using a dynamic sql i.e.
DECLARE #searchstring VARCHAR(500)
DECLARE #str VARCHAR(MAX)
SELECT #str = 'SELECT * FROM Table1 WHERE ' + #searchstring
EXECUTE #str
What I need is I want to select one column value from above dynamic sql to pass in a different SP
Let's say I need ID column value and pass it to another sp named GetAnotherData #Ids. How can I do that?
well you can go with Alexander Fedorenko example, but if you don't want to create any temp tables, you can use output xml parameter to pass your ids:
declare #stmt nvarchar(max), #Data xml, #searchstring nvarchar(max)
select #stmt = '
select #Data = (
select id
from Table1
where ' + #searchstring + '
for xml raw(''Data'')
)
'
exec sp_executesql
#stmt = #stmt,
#params = N'#Data xml output',
#Data = #Data output
select
T.C.value('#id', 'int') as id
from #Data.nodes('Data') as T(C)
sql fiddle demo
The following example creates a user-defined table type that has one Id column. Further the INSERT #RetIds EXEC(#STR) statement fills the parameter list, and then passes the values to a stored procedure
CREATE TYPE RetIds AS TABLE
(
Id int
)
DECLARE #searchstring varchar(500) = 'AND SearchCol = 1'
DECLARE #str varchar(max)
SELECT #str ='SELECT Id FROM dbo.test6 WHERE 1 = 1 ' + #searchstring
DECLARE #RetIds AS RetIds
INSERT #RetIds
EXEC(#str)
EXEC dbo.ExecIds #RetIds
See demo on SQLFiddle
I am trying to understand how the single quotes work in SQL.
All I want to achieve is
INSERT INTO LOGTABLE
(ID,
ROLLNO)
VALUES ('E8645F55-A18C-43EA-9D68-1F9068F8A9FB',
28)
Here ID is a uniqueidentifier field and rollNo is an int.
So I have this sample test code:
set #query = '
insert into fileLog
(
id,
rollNo
)
values
('+
'''' + NEWID() + '''' + ',' + 28 +
')'
print #query
I have tried several combination of single quotes left and right but nothing works. I would really appreciate if someone could solve this. But in particular I wanted to know how many single quotes are required on both sides of a string to get something like 'SQL'.
Thanks
My question is: Why are you using dynamic SQL? It's one of those techniques that is useful in some situations, but can be abused easily.
As for the answer to your question, I use a technique to help minimize the flipping in and out of SQL construction:
DECLARE #query VARCHAR(MAX)
SET #query = '
insert into fileLog
(
id,
rollNo
)
values
(''|NEWID|'', |INT|)'
SET #query = REPLACE(#query, '|NEWID|', NEWID())
SET #query = REPLACE(#query, '|INT|', 28)
PRINT #query
(I'm going to assume you need dynamic SQL for reasons not obvious in the question, since this doesn't seem to require dynamic SQL at all.)
As #Gidil suggested, the problem here is trying to treat a uniqueidentifier as a string. In this case, there really isn't any reason to declare NEWID() in the outer scope, since you can simply say:
SET #query = 'INSERT ... VALUES(NEWID(), 28);';
PRINT #query;
Now, you should be using NVARCHAR(MAX) as your parameter, because ultimately you should be executing this using sp_executesql, not EXEC().
If you need to have a literal you can double up the quotes:
DECLARE #string VARCHAR(32);
SET #string = 'foo';
SET #query = N'INSERT ... VALUES(''' + #string + ''', 28);';
However I find it more readable to use CHAR(39):
SET #query = N'INSERT ... VALUES(' + CHAR(39) + #string + CHAR(39) + ', 28);';
And even better is to not append these variables to a string anyway. You should be using properly typed parameters where possible.
DECLARE #query NVARCHAR(MAX);
DECLARE #string VARCHAR(32), #newid UNIQUEIDENTIFIER, #id INT;
SELECT #string = 'foo', #newid = NEWID(), #id = 28;
SET #query = N'INSERT ... VALUES(#string, #newid, #id);';
EXEC sp_executesql #query,
N'#string VARCHAR(32), #newid UNIQUEIDENTIFIER, #id INT',
#string, #newid, #id;
It's bulkier, sure, but it's much safer from SQL injection and it lets you stop trying to figure out and deal with the hassle of embedding single quotes into the string...
Try this:
DECLARE #query VARCHAR(MAX)
SET #query = ' insert into fileLog ( id, rollNo ) values (' + '''' + Cast(Newid() AS VARCHAR(100)) + ''''
+ ',28)'
PRINT #query
The problem isn't the quotes, but the data types.
NEWID isn't a string and neither is the number 28.
Good luck!
Unless you need dynamic SQL for some reason, you can probably just do this:
insert into fileLog
(
id,
rollNo
)
values
(
NEWID(),
28
)
I have string from which I have to extract substring and query on their values.
declare #str varchar(max)
#str='Hello,world,continent,nation,city'
select * from mytable
where col_word in(SELECT REPLACE(#str,',',''','''))
The sub query
SELECT REPLACE(#str,',',''',''')
results in
Hello,'world','continent','nation','city
I want the above result be enclosed by single quotes so that it can work for IN
But this returns only for first col_word value Hello which is first substring in #str.
What should I do ?
Try this:
You cannot make part of your query as string. We have to make the whole query as a string, then execute it with EXEC() command.. or sp_executesql stored procedure. The latter is recommended.
declare #str varchar(max);
select #str='Hello,world,continent,nation,city';
SELECT #str=''''+REPLACE(#str,',',''',''')+''''
exec('select * from mytable where col_word in('+#str +')')
Try this:
declare #str varchar(max)
declare #pattern varchar(max)
SET #str='Hello,world,continent,nation,city'
SELECT REPLACE(#str,',',''',''')
SET #pattern = REPLACE('Hello,world,continent,nation,city', ',', ''',''')
EXEC('select * from mytable where col_word in(''' + #pattern + ''')')
I want to "concatenate" all the "Text"-rows into one single row and get one row as a result. Is this even possible? I use MSSQL Server 2005.
Use FOR XML PATH:
SELECT [Text]+' ' AS 'text()' FROM _table FOR XML PATH('')
Another option - use string concatenation:
DECLARE #s nvarchar(max)
SELECT #s = ISNULL(#s, '') + t + ' ' FROM _table OPTION (MAXDOP 1)
SELECT #s
Please note that the latter one isn't guaranteed to work, afaik, officially the behaviour of "#s = #s + ..." for multi-row resultset is undefined.
MAXDOP 1 hint is used here to prevent the optimizer from creating a parralel execution plan, as this will yield an incorrect result for sure.
I believe you're looking for something like this:
DECLARE #string nvarchar(max)
SET #string = N''
SELECT #string = #string + [Text] + N' ' FROM [YourTable]
SELECT #string
This will concatenate all of the values for the [Text] column into a single variable. You can then select the variable to retrieve all of the values in a single row.
Something like:
DECLARE #result varchar(max)
SELECT #result = COALESCE(#result + ' ','') +[Text] FROM [Table]
SELECT #result