Showing posts with label dynamic. Show all posts
Showing posts with label dynamic. Show all posts

Friday, March 30, 2012

how to construct dynamic sql in stored procedure

Hi Guys.

I want to construct a SQL in stored procedure in the following way. Please guide me how to achive this

CREATE PROCEDURE P_SAMPLE

(

@.P_ONE NVARCHAR(240)

)

AS

BEGIN

DECLARE

@.TMP_CNT INT

BEGIN

EXEC ('SELECT @.TMP_CNT = 1')

-- PRINT @.TMP_CNT

END

END

Regards

Mani

Use sp_executesql to pass/retrive the data to/from dynamic sql. Exec won’t help you here.

Code Snippet

DECLARE @.TMP_CNT INT

DECLARE @.SQL as NVarchar(4000)

DECLARE @.Params as NVarchar(4000)

Set @.SQL = N'SELECT @.TMP_CNT = 1'

Set @.Params = N'@.TMP_CNT as Int OUTPUT'

Exec sp_executesql @.SQL, @.Params, @.TMP_CNT OUTPUT

Select @.TMP_CNT

|||

here is one of the best resource in Dynamic sql

http://www.sommarskog.se/dynamic_sql.html

Madhu

Wednesday, March 7, 2012

How to compare dynamic variable in proc

I have a table with 52 columns named 'Week1', 'Week2' etc. with values 1, 0 etc. I want to check values in each column. I have following lines in my procedure.

Declare @.l_str varchar(50),
@.l_count int

Select @.l_count = 1
Select @.l_str = 'Week' + Convert(varchar, @.l_count)
Now how do I compare the value stored in the @.l_str which should be wither 0 or 1 and not 'Week1'?

Is there any better method to compare read these 52 table variables?

Thanks in advanceCompare with what?|||Compare to check what does Week1 holds 1 or 0?|||I have a table with 52 columns named 'Week1', 'Week2' etc. with values 1, 0 etc.

It may be the liquid lunch, but

BBBBBBBBBWWWWWWWWWWWWWWWWWAAAAAAAAAAAAHHHAAHAHAHA