简体   繁体   中英

Sql Server SP_EXECUTESQL

I'm trying to use sp_executesql , but I have a certain question, that although I researched I found a possible solution.

In some part of the SQL script I have the following statement:

EXECUTE sp_executesql 
          @sql_troca, 
          N '@CD_ITEM char(7)', 
          @cd_item = @cd_item

And it works.

But in line with the parameters

@cd_item = @cd_item 

I wanted to dynamically build, already have strategic statement that looks like this:

EXECUTE sp_executesql 
          @sql_troca, 
          N '@CD_ITEM char(7)', 
          N ' @cd_item = @cd_item '

but when running, an error shows up:

The parameterized query @CD_ITEM char(7)

How can I pass the parameters as a string? or is there really no possibility

Your code is trying to pass the literal string ' @cd_item = @cd_item ' as the value of the first parameter. The first parameter is defined as char(7) , and you're trying to pass it a 20-character string, which is why you're getting an error message.

However, increasing the length of the parameter to 20 won't fix the problem; you'll just be passing the literal string as the value of the first parameter, when you actually want to pass the value of a local variable.

There is no way to pass the parameters as a string. That's not how sp_executesql works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM