How to set a variable which include another variable in dynamic sql - sql

Here is my Query:-
declare #a nvarchar (150)
declare #b nvarchar (100)
set #b= 'Test'
set #a =('select COUNT(*) from ' +#b + '.dbo.t_ddl_log')
exec (#a)
if (#a='0')
print 'True'
else
print 'False'
It is always printing False even though it should print True.
If I change the variable #b with Test in my count query it works fine.
What is the problem in the query??

The problem is not with the query - the problem is that you mistakenly think that #a should hold the results of the query, while in fact it holds the query itself.
You can use sp_executeSql to get the results you want:
declare #a nvarchar (150),
#b nvarchar (100),
#c int
set #b= 'Test'
set #a =('select #count = COUNT(*) from ' +#b + '.dbo.t_ddl_log')
exec sp_executeSql #a, N'#count int output', #c = #count output
if (#c= 0)
print 'True'
else
print 'False'

Related

Set parameter inside SQL server execute command

I want to assign the value to a parameter PRGMREFID inside the if-else condition using the execute commands. Each time I try to do that, it shows NULL value assigned to PRGMREFID parameter, and if I place its declare command inside SEt #C, I do not find the parameter PRGMREFID outside of if-else. How to do that pls help.
Below is the patch.
DECLARE #C VARCHAR(MAX)
DECLARE #PRGMREFID VARCHAR(10)
IF 1=1
SET #C = 'set '+ (#PRGMREFID) +' = ''X'' '
else
set #c = ''
EXECUTE(#C)
select #PRGMREFID
To do what you are trying to do you need to use sp_executesql and define the parameter as an output parameter in order to have the value returned to your session's local variable:
DECLARE #C NVARCHAR(MAX)
DECLARE #PRGMREFID VARCHAR(10)
if 1=1
set #C = 'set #PRGMREFID = ''X'' '
else
set #C = ''
exec sp_executesql #C, N'#PRGMREFID VARCHAR(10) output', #PRGMREFID output
select #PRGMREFID

Set declared parameter value and CONCAT column name

I have table with columns Char1, Char2, Char3..... Each of these columns contain some value. I declared variable #i and in while loop I'm trying to concat it to the Char table column name.
Also, I declared parameter #current in my query and then I'm trying to set its value in the query.
set #tmp = cast(#i as varchar(2))
select #current = 'Char' + #tmp
from SerialNumberFormat
where Example = 'XXXXXXXXXX'
When I execute the query #current has value Char1, Char2, Char3...etc, instead the value of the column.
How I can set column value instead column name in #current?
select #current = Concat(Char1, Char2, Char3)
from SerialNumberFormat
where Example = 'X59AA419010045'
Solution which I found and works for me is executing sp_executesql stored procedure
set #SQL = N'select #currentOUT = Char' + #tmp + ' from SerialNumberFormat
where Example = ''XXXXXXXXX'''
SET #ParmDefinition = N'#currentOUT nvarchar(10) OUTPUT'
exec sp_executesql #SQL, #ParmDefinition, #currentOUT = #current OUTPUT
select #current
Dynamic Sql like this may work for you
Declare #i int = 5
Declare #tmp varChar (10) = #i
Declare #Sql nVarChar(Max) = 'select Char' + #tmp + '
from SerialNumberFormat
where Example = ''X59AA419010045'''
exec sp_executesql #Sql

SQL Server EXEC OUTPUT always NULL

I am using an EXEC statement and the OUTPUT is always NULL. This is an issue for me. I need some way of identifying whether or not an INSERT statement succeeded. Alternatively, I could remove the #bogusTable and INSERT portion of the query and just test if the underlying SELECT statement gathered any results, but I always got NULL on that OUTPUT as well.
Here is the query:
SET #mainQuery = '
DECLARE #bogusTable TABLE(
someField VARCHAR(MAX) NULL
);
INSERT INTO #bogusTable SELECT someField FROM someTable WHERE anINT = ' #randINT
EXEC sp_executesql #mainQuery, N'#tempParam INT OUTPUT', #tempParam=#someInt OUTPUT
IF (#someInt IS NULL)
BEGIN
--This always executes even when the INSERT statement in #mainQuery doesn't insert anything
END
You're not setting #tempParam in your dynamic sql, so of course it is null. You can set it to ##rowcount to see if any rows were inserted.
Try this:
declare #randINT int;
--set #randINT here!
declare #mainQuery nvarchar(max);
declare #someInt int;
SET #mainQuery = '
DECLARE #bogusTable TABLE(
someField VARCHAR(MAX) NULL
);
INSERT INTO #bogusTable SELECT someField FROM someTable WHERE anINT = #randINT;
select #tempParam = ##rowcount;'
EXEC sp_executesql #mainQuery, N'#randINT int, #tempParam INT OUTPUT', #randINT=#randINT, #tempParam=#someInt OUTPUT
IF (#someInt > 0)
BEGIN
--This will only execute if something was inserted
select #someInt as row_count;
END
Notice, I also brought #randINT into the parameters correctly.
#BeanFrog was pretty close.
This is what I needed:
SET #mainQuery = '
DECLARE #bogusTable TABLE(
someField VARCHAR(MAX) NULL
);
INSERT INTO #bogusTable SELECT someField FROM someTable WHERE anINT = ' + #randINT + ';
SET #tempParam = ##ROWCOUNT;'
EXEC sp_executesql #mainQuery, N'#tempParam INT OUTPUT', #tempParam=#someInt OUTPUT
IF (#someInt = 0)
BEGIN
--This executes correctly now
END

SQL "if exists..." dynamic query

Suppose I have a query stored in a variable like this (it's actually dynamically populated and more complex, but this is for demonstration purposes):
DECLARE #Query VARCHAR(1000) = 'SELECT * FROM dbo.MyTable'
Is there a way to check if the query would return any results? Something like this, but this doesn't work:
IF EXISTS (#Query)
BEGIN
-- do something
END
The only way that I can think of to do this is to put the results in a temp table and then query from that, but that is not ideal because the columns in the dynamic query can vary and I really don't need the temp table at all for any reason other than checking whether some rows would be returned. Is there a better way?
Try Executing the Dynamic query and use ##RowCount to find the existence of rows.
DECLARE #Query NVARCHAR(1000) = 'SELECT * FROM [dbo].[Mytable]',
#rowcnt INT
EXEC Sp_executesql #query
SELECT #rowcnt = ##ROWCOUNT
IF #rowcnt > 0
BEGIN
PRINT 'row present'
END
Try this:
DECLARE #Query NVARCHAR(1000) = 'SELECT #C = COUNT(*) FROM dbo.MyTable'
DECLARE #Count AS INT
EXEC sp_executesql #Query, N'#C INT OUTPUT', #C=#Count OUTPUT
IF (#Count > 0)
BEGIN
END
I know this answer is too late. but, I'm leaving this here to help someone else to use IF EXISTS with a dynamic query.
This is how you should do it with dynamic queries.
DECLARE #Query VARCHAR(MAX)
SET #Query = 'SELECT * FROM [dbo].[MyTable]'
SET #Query = 'IF EXISTS (' + #Query + ')
BEGIN
-- do something
print ''1''
END
ELSE
BEGIN
-- do something else
print ''0''
END
'
exec (#Query)
Hope this helped someone. Vote if it did :)
You can use EXEC to execute sql statement, then call ##ROWCOUNT which Returns the number of rows affected by the last statement, to check row exists in sql select stetement.
DECLARE #Query VARCHAR(1000) = 'SELECT * FROM dbo.MyTable',#hasRow int
EXEC (#Query)
SELECT #hasRow =##ROWCOUNT // Returns the number of rows affected by the last statement
PRINT #hasRow
IF #hasRow > 0
BEGIN
Print 1
END
BEGIN
Print 2
END
Hi I think that only way is to put IF EXISTS part into code of execution. My case is to stop execution in point when select affects at least one row, that is goal of IF EXISTS.
Little example that saves reading all records covering by condition to first occurence:
set nocount off;
drop table if exists #temp
go
create table #temp (idCol int identity(1,1),someText nvarchar(1))
go
insert into #temp values ('a')
go 25000
declare #query nvarchar(max)
,#resultFork bit
set #query = 'if exists (select * from #temp where idCol % 3 = 0)
set #resultFork=1
else
set #resultFork=0'
print #query
exec sp_executeSQL #query, N'#resultFork int output', #resultFork=#resultFork output
print #resultFork
/*Now U can use #resultFork in simple if condition...
if #resultFork = 1
begin
--
end
else
begin
--
end
*/

return null instead of nothing if there are no results

I have a dynamic sql statement that may or may not return any results. For simplicity sake:
--other SQL statements defining #a
DECLARE #sql varchar(128)
SELECT #sql = 'SELECT a, b FROM c WHERE a = ' + #a
EXEC(#sql)
I want the results if this statement returns any but if EXEC(#sql) does not return any results how can I go about making it return a single null instead?
I am working with MS SQLServer 2000.
EXPLANATION FOR X-ZERO
This is for use in a webapp ajax response. I am not able to alter the backend java framework which requires a resultset of some kind to be returned by the query to the webservice.
SOLUTION
In case anyone else has this question and because it wasn't totally clear in the answers, this worked:
--other SQL statements defining #a
DECLARE #sql varchar(128)
SELECT #sql = 'SELECT a, b FROM c WHERE a = ' + #a
EXEC(#sql)
IF ##ROWCOUNT = 0 SELECT null
You can get the result set record count of the statement with the same WHERE clause, and it it is zero, then you can just return one record with null value.
Example:
declare #sql varchar(128)
declare #a varchar(10);
set #a = '''Some #a value''';
declare #rowcount int;
exec sp_executesql N'select #rowcount=count(*) FROM c WHERE a = ''Some #a value''',
N'#rowcount int output', #rowcount output;
if #rowcount <> 0
begin
select #sql = 'SELECT a, b FROM c WHERE a = ' + #a
end
else
begin
select #sql = 'select null';
end
exec(#sql) ;
Hope this helps you!
If I understand your question, you want the statement to return a single row consisting of null when no rows are returned from the actual query. Or, put another way, you want it to return a value (null) when it's supposed to return no values.
I think what you should do instead is leave the query as-is and get a count of the number of rows returned by the statement. I believe you can do this with the ##rowcount variable after doing the query.
(Disclaimer: What I know about SQL Server I learned from Google and S.O.)
Another option avoiding ##ROWCOUNT would be to save the results in a table variable, then return the results if any exist or NULL if they don't. E.g:
DECLARE #results TABLE(a int, b int)
DECLARE #sql nvarchar(128)
SELECT #sql = 'SELECT a, b FROM c WHERE a = ' + #a
insert into #results
exec (#sql)
IF EXISTS(select top 1 * from #results)
SELECT * from #results
ELSE SELECT null
Look at the SQL Docs on MAX(Transact-SQL). "MAX returns NULL when there is no row to select.". I should give credit to this answer on SqlServerCentral.
DECLARE #sql VARCHAR(128)
SET #sql = 'SELECT MAX(a), MAX(b) FROM c WHERE a = ' + #a
EXEC SP_EXECUTESQL #sql