Tuesday, February 14, 2012

debug a SELECT query and using variable as column name

i have 2 questions:
1. is there any way to watch the a SELECT as it running? i mean to c how it
acts for each value that it meets and how it ignores each value that not
contains in the 'where' clause
2. im trying to run a select query with variable on a COLUMN name for
example :
declare @.a1 nvarchar(100)
set @.a1= 'my_column_name'
select @.a1 from my_table
but im getting a wrong result. im getting a long single column with the
'my_column_name' value in each cell.
how can i use a column name with variable?
Regards,
OrenYou should use Dynamic SQL
declare @.a1 nvarchar(100)
set @.a1= 'my_column_name'
Exec('select '+@.a1+' from my_table')
Madhivanan|||Hi Oren
Point 1:
you have a work arround for this. you can declare a cursor and try printing
each row onw by one. Once all the rows are printed, then you can debug and
check the result.
Point 2:
you can try this as:
declare @.a1 nvarchar(100)
set @.a1= 'my_column_name'
sp_executesql 'select ' + @.a1 + ' from my_table'
please let me know if u would kine to know anything else
best Regards,
Chandra
http://chanduas.blogspot.com/
http://groups.msn.com/SQLResource/
---
"Oren101" wrote:

> i have 2 questions:
> 1. is there any way to watch the a SELECT as it running? i mean to c how i
t
> acts for each value that it meets and how it ignores each value that not
> contains in the 'where' clause
> 2. im trying to run a select query with variable on a COLUMN name for
> example :
> declare @.a1 nvarchar(100)
> set @.a1= 'my_column_name'
> select @.a1 from my_table
> but im getting a wrong result. im getting a long single column with the
> 'my_column_name' value in each cell.
> how can i use a column name with variable?
> Regards,
> Oren
>|||thanks Chandra .the sp_executesql worked for me.
do u have any code example for my first question?
Regards,
OreN
"Chandra" wrote:
> Hi Oren
> Point 1:
> you have a work arround for this. you can declare a cursor and try printin
g
> each row onw by one. Once all the rows are printed, then you can debug and
> check the result.
> Point 2:
> you can try this as:
> declare @.a1 nvarchar(100)
> set @.a1= 'my_column_name'
> sp_executesql 'select ' + @.a1 + ' from my_table'
> please let me know if u would kine to know anything else
> --
> best Regards,
> Chandra
> http://chanduas.blogspot.com/
> http://groups.msn.com/SQLResource/
> ---
>
> "Oren101" wrote:
>|||I think the QUOTENAME() function could help you out to for the column
name in the select statement which might prevent the need for sp_executesql?
Clint Hill
H3O Software
http://www.h3osoftware.com
Oren101 wrote:
> thanks Chandra .the sp_executesql worked for me.
> do u have any code example for my first question?
> Regards,
> OreN
> "Chandra" wrote:
>|||hi
just try this way:
select Ename, salary, case when ename like 'C%' then 1 else 0 end result
from Employee
if you have 0 in result then the condition is false.
best Regards,
Chandra
http://chanduas.blogspot.com/
http://groups.msn.com/SQLResource/
---
"Oren101" wrote:
> thanks Chandra .the sp_executesql worked for me.
> do u have any code example for my first question?
> Regards,
> OreN
> "Chandra" wrote:
>|||hi,
what i ment was more like debugging so i can c watch the comparing for each
line in the table
"Chandra" wrote:
> hi
> just try this way:
> select Ename, salary, case when ename like 'C%' then 1 else 0 end result
> from Employee
> if you have 0 in result then the condition is false.
> --
> best Regards,
> Chandra
> http://chanduas.blogspot.com/
> http://groups.msn.com/SQLResource/
> ---
>
> "Oren101" wrote:
>|||There's no debugging at the intra-statement level for SQL Server. The closes
t you can come (IMO) is
looking at the query plan.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"Oren101" <Oren101@.discussions.microsoft.com> wrote in message
news:2EAC73B1-CD6E-481A-B9EA-147B7E17EE03@.microsoft.com...
> hi,
> what i ment was more like debugging so i can c watch the comparing for ea
ch
> line in the table
> "Chandra" wrote:
>

No comments:

Post a Comment