views:

531

answers:

2

Hi,

I'm not sure how to resolve this error:

*Procedure or Function 'sp_executesql' expects parameter '@statement', which was not supplied.*

for this query:

DECLARE @a INT 
DECLARE @b VARCHAR 
SET @a = 1

WHILE @a < 30
BEGIN
set @b = @a  
exec sp_executesql update source_temp set pmt_90_day = pmt_90_day + convert(money,'trans_total_'+@b)+N'
    N'where convert(datetime,'effective_date_'+@b)+N' <= dateadd(day,90,ORSA_CHARGE_OFF_DATE)
    and DRC_FLAG_'+@b = 'C'''

SET @a = @a + 1
END

Also, can you help me understand the proper usage of N' and if its done correctly in this statement.

Thanks for your assistance.

A: 

The "sp_executesql" stored proc expects a single string parameter @statement to execute.

Your string is totally out of whack here..... you need to prefix any "fixed" string parts with a N'......' to make them Unicode strings, but this is definitely not properly the case here.

I think you might want to try this:

DECLARE @a INT 
DECLARE @b VARCHAR(2)

SET @a = 1

DECLARE @statement NVARCHAR(500)

WHILE @a < 30
BEGIN
    SET @b = CAST(@a AS VARCHAR(2))

    SET @statement = 
     N'update source_temp set pmt_90_day = pmt_90_day + ' + 
             'convert(money, ''trans_total_' + @b + ''') ' + 
             'where convert(datetime, ''effective_date_' + @b + ''')' +
             ' <= DATEADD(DAY, 90, ORSA_CHARGE_OFF_DATE) ' +
             'and DRC_FLAG_' + @b + ' = ''C'''

    exec sp_executesql @statement

    SET @a = @a + 1
END

Does this work and do what you expect it to do??

Explanation: the N'.........' delimits the whole string which contains the SQL statement. Any apostrophe inside that has to be duplicated ("escaped"). I hope I understood your logic correctly.

Marc

marc_s
I'm trying to run my update dynamically in a loop so each iteration updates the fields by an increment of 1. Its a reference from http://stackoverflow.com/questions/753947/using-while-loop-for-sql-server-update
homerjay
ex: iteration 1 uses effective_date_1, iteration2 uses effective_date_2, etc
homerjay
Closer :D getting this error Must declare the scalar variable @b, I don't know why since its declared up top
homerjay
Phew - dynamic SQL is almost quite messy, especially if you try to "concat together" your field names and stuff.....
marc_s
Oh were so close, "Incorrect syntax near '<'"
homerjay
Painfully close.." Msg 245, Level 16, State 1, Line 1Conversion failed when converting the varchar value 'effective_date_' to data type int. "
homerjay
Phew - this is long a long-distance jigsaw puzzle without seeing the pieces, just calling out what to do :-)
marc_s
I know, I really appreciate your help.
homerjay
So close.. " Conversion failed when converting datetime from character string. "
homerjay
Well, but this seems like there's a probelm in you "effective_date_xxx" columns - are those really valid dates? All of them? Try running a "SELECT Effective_Date_1, CONVERT(DATETIME, Effective_Date_1)" and so on and see if all works - if not, fix your Effective_date column
marc_s
Your right many of the outer effective date fields are empty so the convert evaluates to '1900-01-01 00:00:00.000' How should I fix that?
homerjay
Are the fields empty meaning "NULL"? In that case you could add an ISNULL(Effective_Date_x, some_default_date) clause to the statement
marc_s
Or could you exclude all dates that are NULL ?? Just skip those rows?
marc_s
Thank you soooo much for your help
homerjay
+1  A: 

The sp expects a variable of type string, not a SQL statement. Wrap you SQL in single quotes:

 exec sp_executesql 'some SQL statement';

When you do that, escape any single quotes in the statement by replacing each single quote with two single quotes:

exec sp_executesql 'select 'AB' from dual';  -- wrong

exec sp_executesql 'select ''AB'' from dual';  -- right

I have no idea what N is. What do you think it is? Is it some sort of cast to a character set? Why do you think it's necessary?

tpdi
The N'' denotes a UNICODE (NVARCHAR) string
marc_s