Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing Null value parameter to dynamic SQL query. How can this be successfully executed?

Tags:

sql

sql-server

I am executing a dynamic SQL query. where parameters are passed dynamically. I first wrote similar to following code.

DECLARE @Id nvarchar(max);
DECLARE @query nvarchar(max);
SET @Id ='RMH_108'
SET @query ='Select * 
             FROM [A06].[Syn_RMDemand]
             WHERE RMHierarchyId =' + @Id
PRINT @query
EXEC(@query)

Error

Then I wrote this.

DECLARE @Id nvarchar(max);
DECLARE @query nvarchar(max);
SET @Id ='RMH_108'
SET @query ='Select * 
             FROM [A06].[Syn_RMDemand]
             WHERE RMHierarchyId = '''+@Id+''''
PRINT @query
EXEC(@query)

ERROR

This time code successfully executed. Since parameter can be empty I need to convert that to null. I modified the code and wrote this

DECLARE @Id nvarchar(max);
DECLARE @query nvarchar(max);
SET @Id = ''
SET @Id = NULLIF(@Id,'')
-- COMMENTED SET @Id ='RMH_108'

SET @query ='Select * FROM [A06].[Syn_RMDemand]
             WHERE RMHierarchyId = '''+@Id+''''
PRINT @query
EXEC(@query)

Error

No errors. Query crashed.
Does anybody come up with this sort of problem ?

like image 635
Aslam Jiffry Avatar asked Jun 24 '15 06:06

Aslam Jiffry


People also ask

How do you pass dynamic parameters in SQL query?

Executing dynamic SQL queries Dynamic SQL queries are those built at runtime based on one or more variable values. To execute those queries, we must concatenate them into one SQL statement and pass them as a parameter to the sp_executesql stored procedure.

How dynamic SQL can be executed?

Executing dynamic SQL using sp_executesql sp_executesql is an extended stored procedure that can be used to execute dynamic SQL statements in SQL Server. we need to pass the SQL statement and definition of the parameters used in the SQL statement and finally set the values to the parameters used in the query.

What are the three ways that dynamic SQL can be executed?

What are the three ways that Dynamic SQL can be executed? Writing a query with parameters. Using EXEC. Using sp_executesql.

How do you handle NULL parameters in SQL?

Handling SQL NULL values with Functions ISNULL(): The ISNULL() function takes two parameters and it enables us to replace NULL values with a specified value. The expression parameter indicates the expression which we want to check NULL values.


2 Answers

You should use sp_executeSQL and remove string concatenation like this. I am assuming that if NULL is passed you want all the rows to be returned.

Query

DECLARE @Id nvarchar(max);
DECLARE @query nvarchar(max);
SET @Id ='RMH_108'SET @query ='Select * 
                               FROM [A06].[Syn_RMDemand]
                               WHERE RMHierarchyId = @Id OR @ID IS NULL'
PRINT @query
EXEC sp_executeSQL @query,N'@Id NVARCHAR(MAX)',@Id
like image 147
ughai Avatar answered Oct 04 '22 12:10

ughai


Try the following:

SET @query ='Select * FROM [A06].[Syn_RMDemand]'
IF @Id IS NOT NULL
   SET @query = @query + ' WHERE RMHierarchyId = '''+@Id+''''

If the parameter comes from client you better do with parametrized query(see @ughai's answer) in order to exclude sql injection possibility...

like image 27
Giorgi Nakeuri Avatar answered Oct 04 '22 12:10

Giorgi Nakeuri