Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 using sp_executesql and parameters

Author  Topic 

mcotter
Starting Member

3 Posts

Posted - 2008-02-03 : 16:22:22
I created a SQL procedure that uses an input parameter. This procedure also uses sp_executesql.

----------------------------------------
Create PROCEDURE [dbo].[PersonAddress] (
@AddressID int
)

AS

declare @sql nvarchar(MAX)

/*select * from Person.Address where AddressID = @AddressID */

set @sql = N'select * from Person.Address where AddressID = @AddressID'
exec sp_executesql @sql, N'@AddressID int'
--------------------------------------------------

When I use just the select statement, the procedure works. When I use sp_executesql,I get the following error:

The parameterized query '(@AddressID int)select * from Person.Address where AddressID = @' expects the parameter '@AddressID', which was not supplied.

Can anyone identify the issue?

mcotter
Starting Member

3 Posts

Posted - 2008-02-04 : 10:02:02
I found the problem. The following command should be
--
exec sp_executesql @sql, N'@AddressID int', @AddressID
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-02-10 : 22:21:46
read this for more info on Dynamic SQL http://www.sommarskog.se/dynamic_sql.html


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -