473,386 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

IsNull Evaluation within EXEC

Hi,

I seemed to me IsNull Evaluation within EXEC fails. Here's some more
detail of the problem.

-- goal: provide one parameter (of various value) to generate a
-- report
declare @col4 varchar(30)

select @col4 = null
-- pls note, the @col4 var is default to null but may likely
-- have a value

exec ('select col1, col2, col3, -- next dynamic col
' + @col4 + ',
col5, col6
count(*) as total
from FACT_TBL
where 1=1
-- THE FOLLOWING CONDITION EVALUATION FAILED
-- in the sense that if the parameter is not called query does
-- not return any result set, which is wrong
and COL4 = IsNull('''+@COL4+''',COL4)
group by '+@COL4+', col5
')

Jul 23 '05 #1
8 6393
NickName (da****@rock.com) writes:
I seemed to me IsNull Evaluation within EXEC fails. Here's some more
detail of the problem.

-- goal: provide one parameter (of various value) to generate a
-- report
declare @col4 varchar(30)

select @col4 = null
-- pls note, the @col4 var is default to null but may likely
-- have a value

exec ('select col1, col2, col3, -- next dynamic col
' + @col4 + ',
col5, col6
count(*) as total
from FACT_TBL
where 1=1
-- THE FOLLOWING CONDITION EVALUATION FAILED
-- in the sense that if the parameter is not called query does
-- not return any result set, which is wrong
and COL4 = IsNull('''+@COL4+''',COL4)
group by '+@COL4+', col5
')


This can never work. Normally concatenation with NULL yields a NULL value,
so all you would get is EXEC(NULL). However, that rull does not seem to
apply to EXEC(), so instead the NULL value is substituted with the
empty string, leaving you with isnull('', COL4) of which the result is
''.

You could save the show with

nullif(IsNull('''+@COL4+''', ''''), COL4)

Then again, if @COL4 is NULL, then the SELECT list and the GROUP BY
clause will give you a syntax error.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #3
Ahe, thanks, but I did no formulate the problem exactly. It should
really be written as the following:

-- goal: provide one parameter (of various value -- any of a provided
list of columns) to generate a
-- report, ONE column selection is mandatory or it's a mandatory param
declare @col4 varchar(30),
@colX varchar(30), -- optional condition
@colY int -- optional condition

select @col4 = null
-- pls note, the @col4 var is default to null but may likely
-- have a value

exec ('select col1, col2, col3, -- next dynamic col
' + @col4 + ',
col5, col6
count(*) as total
from FACT_TBL
where 1=1
-- THE FOLLOWING CONDITION EVALUATION FAILED
-- in the sense that if the parameter is not called query does
-- not return any result set, which is wrong
and colX = IsNull('''+@colX+''',colX)
and colY = IsNull('''+@colY+''',colY)
/* REPLACING THE ABOVE two conditions with
and colX = NullIf(IsNull('''+@colX+''',''''),colX)
and colY = NullIf(IsNull('''+@colY+''',''''),colY)
does not seem to help. There must be a way to do this, giving user a
choice to pick one column from many while making at least one or two
columns mandatory (default) with a sp.
*/
group by '+@COL4+', col5
')

Many thanks as usual.

Jul 23 '05 #4
NickName (da****@rock.com) writes:
Ahe, thanks, but I did no formulate the problem exactly. It should
really be written as the following:

-- goal: provide one parameter (of various value -- any of a provided
list of columns) to generate a
-- report, ONE column selection is mandatory or it's a mandatory param
...
/* REPLACING THE ABOVE two conditions with
and colX = NullIf(IsNull('''+@colX+''',''''),colX)
and colY = NullIf(IsNull('''+@colY+''',''''),colY)
does not seem to help. There must be a way to do this, giving user a
choice to pick one column from many while making at least one or two
columns mandatory (default) with a sp.
*/


Now there is a @colX and a @colY. I think that I understand less
than before. Thus, I resort to the standard recommendaton that you
include:

o CREATE TABLE statements for your table.
o INSERT statements with sample data.
o The desired result given the sample.

For this case you would also have to supply the desited interface
for your stored procedure, as well as an example on you intend to call it.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #5
Sounds fair. Please see below. Thanks alot.

/*
-- ENV: MS SQL Server 2000; current db: northwind
-- ddl
create table #myTBL (col1 int, col2 varchar(10), col3 bit, col4
datetime);

-- dml: data population
insert into #myTBL
values(1,'abc',0,getDate());

insert into #myTBL
values(2,'efg',1,'01/02/2004');

insert into #myTBL
values(3,'hij',0,'03/12/2004');

insert into #myTBL
values(4,'klm',0,'02/14/2004');

insert into #myTBL
values
(1,'nlo',0,'05/14/2004');

insert into #myTBL
values
(3,'opq',1,'08/24/2004')
*/

/*
-- dml: get sample data
select *
from #myTBL

-- result set
col1 col2 col3 col4

----------- ---------- ----
------------------------------------------------------
1 abc 0 2005-04-25 11:29:48.473
2 efg 1 2004-01-02 00:00:00.000
3 hij 0 2004-03-12 00:00:00.000
4 klm 0 2004-02-14 00:00:00.000
1 nlo 0 2004-05-14 00:00:00.000
3 opq 1 2004-08-24 00:00:00.000
*/

/* business case/requirements:
a) provide one mandatory parameter (of selecting one column from a
few);
b) provide optional conditional statement (such as col3)
*/

-- sql stmt to support the above objective

declare @dCOL varchar(30), @col3 bit -- optional condition

select @dCOL = 'col1'
-- pls note, the @dCOL var is default to null but MUST have a value
select @col3 = null
/*
select @col3 = 0
select @col3 = 1
NOTE:
set select @col3 = null
or
select @col3 = 0
PRODUCES SAME RESULTSET, which means
NULL and 0 are treated the same, hmm, how come?
*/

exec ('select ' + @dCOL + ', col3, count(*) as total
from #myTBL
where 1=1
and col3 = IsNull('''+@col3+''',col3)
-- NullIf(IsNull('''+@col3+''',''''),col3)
group by '+@dCOL+', col3
')

Jul 23 '05 #6
NickName (da****@rock.com) writes:
/*
select @col3 = 0
select @col3 = 1
NOTE:
set select @col3 = null
or
select @col3 = 0
PRODUCES SAME RESULTSET, which means
NULL and 0 are treated the same, hmm, how come?
*/

exec ('select ' + @dCOL + ', col3, count(*) as total
from #myTBL
where 1=1
and col3 = IsNull('''+@col3+''',col3)
-- NullIf(IsNull('''+@col3+''',''''),col3)
group by '+@dCOL+', col3
')


I'm still not sure that I understand - you forgot to include
expected results for various inputs - but instead of solving
everything in one SQL String, why not build the SQL string
piece by and add extra conditions as needed?

Since col3 is a bit, and you isnull plays with string, things
are likely to be messy.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #7
"you isnull plays with string", Interesting point that I previously
wasn't aware of, however, my following testing seems to prove that even
with pure text strings, NULL evaluation within EXEC still fails.
Please see below. TIA.
/*
-- ddl
create table #myTBL (col1 int, col2 varchar(10), col3 bit, col4
datetime, col5 varchar(20));

-- dml: data population
insert into #myTBL
values(1,'abc',0,getDate(),'zzyyxx');

insert into #myTBL
values(2,'efg',1,'01/02/2004','ttssrr');

insert into #myTBL
values(3,'hij',0,'03/12/2004','rrppoo');

insert into #myTBL
values(4,'klm',0,'02/14/2004','qqppoo');

insert into #myTBL
values
(1,'nlo',0,'05/14/2004','nnmmll');

insert into #myTBL
values
(3,'opq',1,'08/24/2004','kkjjii')
*/

/*
-- dml: get sample data
select *
from #myTBL

-- result set
col1 col2 col3 col4
col5
----------- ---------- ----
------------------------------------------------------
--------------------
1 abc 0 2005-04-26 09:29:16.817
zzyyxx
2 efg 1 2004-01-02 00:00:00.000
ttssrr
3 hij 0 2004-03-12 00:00:00.000
rrppoo
4 klm 0 2004-02-14 00:00:00.000
qqppoo
1 nlo 0 2004-05-14 00:00:00.000
nnmmll
3 opq 1 2004-08-24 00:00:00.000
kkjjii

(6 row(s) affected)
*/

/* business case/requirements:
a) provide one mandatory parameter (of selecting one column from a
few);
b) provide optional conditional statement (such as col3)
*/

-- sql stmt to support the above objective

declare @dCOL varchar(30), @col5 varchar(20) -- optional condition

select @dCOL = 'col1'
-- pls note, the @dCOL var is default to null but MUST have a value
select @col5 = 'ttssrr'
-- select @col5 = null

exec ('select ' + @dCOL + ', col5, count(*) as total
from #myTBL
where 1=1
and col5 = IsNull('''+@col5+''',col5)
group by '+@dCOL+', col5
')

/*
-- execution result of the above stmt with @col5 set to 'ttssrr'
col1 col5 total
----------- -------------------- -----------
2 ttssrr 1
*/

/*
-- execution result of the above stmt with @col5 set to null
col1 col5 total
----------- -------------------- -----------

Expected result:
6 rows (for the col5 condition would be "and col5 = col5")
*/

Jul 23 '05 #8
Erland,

Just an update, I figured it out. :)

Don

Jul 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Andreas Paasch | last post by:
According to the manual for PHP, I should be able to run a shell command within php. I'm trying to copy some php files from one location to another one using exec() but fail. ...
11
by: Bhushit Joshipura | last post by:
This post contains one question and one proposal. A. May I know why order of evaluation of arguments is not specified in C/C++? I asked a question in comp.lang.c++ for the following...
20
by: Ilias Lazaridis | last post by:
" A cooperation between Sun Microsystems and IBM&Co. in conjunction with liberal & high evolutive communities would result in an nearly unbeatable programming platform. My evaluation has shown:...
1
by: rashika | last post by:
Hi : Can i call 2 procs within one task? I have sp_proc1 ? (and have declared one global variable as input parameter) now i have another sp_proc2 which uses same input parameter but if i...
31
by: NickName | last post by:
/* goal: dynamic evaluation of table row platform: sql 2000 */ use northwind; declare @tbl sysname set @tbl = 'customers' EXEC('select count(*) from ' +@tbl)
21
by: dragoncoder | last post by:
Consider the following code. #include <stdio.h> int main() { int i =1; printf("%d ,%d ,%d\n",i,++i,i++); return 0; }
77
by: berns | last post by:
Hi All, A coworker and I have been debating the 'correct' expectation of evaluation for the phrase a = b = c. Two different versions of GCC ended up compiling this as b = c; a = b and the other...
2
by: Graham Menhennitt | last post by:
I have a large Python 2.5 program that I want my users to be able to "extend" using a Python script. However, I want their script to run in a sandbox within the overall program so that they only...
54
by: Rasjid | last post by:
Hello, I have just joined and this is my first post. I have never been able to resolve the issue of order of evaluation in C/C++ and the related issue of precedence of operators, use of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.