April 23, 2008 at 7:38 am
Experts,
I have one senario.
create table #t1
(
Eno int,
Age int null
)
insert into #t1(Eno)
select 1
union all
select 2
union all
select 3
insert into #t1
select 4,5000
select * from #t1
Output:
Eno Age
----------- -----------
1 NULL
2 NULL
3 NULL
4 5000
declare @NULL int
declare @Nm varchar(50)
select @Nm = 'Age'
select @NULL = count(*) from #t1 where (@Nm) is null
select @NULL
is showing '0' instead of '3'.How ?
Inputs are welcome !
karthik
April 23, 2008 at 7:50 am
Because the variable @Nm is not null. It is equal to 'Age'
If you want to have a variable column specified in the where clause, you'll have to use dynamic SQL, with all it's associated problems.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
April 23, 2008 at 8:00 am
Karthik -
You're trying to backdoor in some dynamic SQL without writing it up as dynamic SQL. It's not going to use the variable as a column name, it's simply going to evaluate it as a scalar value.
Set it up the right way, using dynamic SQL.
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
April 23, 2008 at 8:01 am
Carl Federl (4/23/2008)
Why are you expecting a value of 3 ?
He wants to specify the column name in a variable.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
April 23, 2008 at 8:08 am
Carl Federl
select @Nm = 'Age'
select @NULL = count(*) from #t1 where (@Nm) is null
As "where (@Nm) is null" will always be false, zero rows will meet the
restriction.
Why and how ? Please explain me.
Why are you expecting a value of 3 ?
Because Age column contains 3 NULL values. Simply i want to return the count of number of NULL in the Age column.
Matt Miller
it's simply going to evaluate it as a scalar value.
Can you explain it how ?
Whethere my code is to be considered as a dynamic sql or not .If not please provide me the proper query to achieve the same task.
karthik
April 23, 2008 at 8:17 am
karthikeyan (4/23/2008)
Carl Federl
select @Nm = 'Age'
select @NULL = count(*) from #t1 where (@Nm) is null
As "where (@Nm) is null" will always be false, zero rows will meet the
restriction.
Why and how ? Please explain me.
Because the variable @Nm has the value 'Age'. Hence, it is not null.
You're asking for the number of rows where the variable has the value null, which is 0.
SQL does not dynamically evaluate column names like that. You will ahve to build up the SQL string and EXEC it.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
April 23, 2008 at 8:30 am
Gila,
Can you tell me how to do it ?
karthik
April 23, 2008 at 12:33 pm
Look up sp_executesql. It allows you to pass parameters in and out of dynamic SQL. I know there's a good example in books online showing exactly that.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
April 24, 2008 at 3:07 am
karthikeyan (4/23/2008)
Carl Federl
select @Nm = 'Age'
select @NULL = count(*) from #t1 where (@Nm) is null
As "where (@Nm) is null" will always be false, zero rows will meet the
restriction.
Why and how ? Please explain me.
Why are you expecting a value of 3 ?
Because Age column contains 3 NULL values. Simply i want to return the count of number of NULL in the Age column.
Matt Miller
it's simply going to evaluate it as a scalar value.
Can you explain it how ?
Whethere my code is to be considered as a dynamic sql or not .If not please provide me the proper query to achieve the same task.
Any Inputs ?
karthik
April 24, 2008 at 4:14 am
use following query
declare @Query nvarchar(500),@parameters nvarchar(500)
set @Query='select @NULL = count(*) from #t1 where '+@Nm+' is null'
set @parameters='@Null int output'
exec sp_executesql @Query,@parameters,@Null=@Null output
print @NULL
insted of
select @NULL = count(*) from #t1 where (@Nm) is null
April 24, 2008 at 4:41 am
Hi Karthick,
I think the problem with your code is you have assigned age like a value for the @Nm, not Age as like a column containing the values
I tried and give my result ,hope it will help u
--in this scenario u have to use a table variable,I have Rewritten your code as follows
declare @NULL int
Declare @Nm table(Age Varchar(50))
insert into @Nm(Age)
select Age from #t1 where Age is null
Select @NULL= count(*) from @nm
select @NULL
2.
u can simply replace age in place of @Nm in the where condition it will give you the expected result too. Or you have to use a table variable as like above.
Regards,
Rajesh
April 24, 2008 at 6:11 am
Thanks Raja.
karthikeyan (4/23/2008)
--------------------------------------------------------------------------------
Carl Federl
select @Nm = 'Age'
select @NULL = count(*) from #t1 where (@Nm) is null
As "where (@Nm) is null" will always be false, zero rows will meet the
restriction.
Why and how ? Please explain me.
Why are you expecting a value of 3 ?
Because Age column contains 3 NULL values. Simply i want to return the count of number of NULL in the Age column.
Matt Miller
it's simply going to evaluate it as a scalar value.
Can you explain it how ?
Whethere my code is to be considered as a dynamic sql or not .If not please provide me the proper query to achieve the same task.
Can anybody explain it ?
karthik
April 24, 2008 at 6:35 am
Yes, and I did. See the last post on page 1.
If there's anything you're still not sure of, ask specifically.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
Viewing 15 posts - 1 through 15 (of 24 total)
You must be logged in to reply to this topic. Login to reply