You did not set @params to the proper parameter string. Add the following
line before EXEC:
SET @params = N'@custid NVARCHAR(50)';
HTH,
Plamen Ratchev
http://www.SQLStudio.com
Thanks Plamen. After changing the error went away with a new error. Could you
please tell me why this error is coming up. Thanks
create procedure customer_process_now
@custid Nvarchar(50),
@site Nvarchar(10)
as
declare @sitedbname Nvarchar(25)
declare @sql Nvarchar(4000)
DECLARE @params NVARCHAR(100);
set @sitedbname = 'ts2' + @site + '_app'
SET @params = N'@custid NVARCHAR(50)';
select @sql = ' select custid, sales_ytd, sales_lstyr
' + 'from ' + @sitedbname + '..tblcustomer '
select @sql = @sql + ' where custid = @custid '
--exec sp_executesql @sql, N'custid Nvarchar(50), N'site Nvarchar(10),
--@custid = @custid, @site = @site
EXEC sp_executesql @sql, @params, @custid = @custid;
select * from tblcustomer
c000001
customer_process_now 'c000001', 'aldkm'
Server: Msg 208, Level 16, State 1, Line 1
Invalid object name 'ts2aldkm_app..tblcustomer'.
> You did not set @params to the proper parameter string. Add the following
> line before EXEC:
[quoted text clipped - 5 lines]
> Plamen Ratchev
> http://www.SQLStudio.com
Plamen Ratchev - 22 Jul 2008 16:59 GMT
Your code generates the following query:
select custid, sales_ytd, sales_lstyr
from ts2aldkm_app..tblcustomer where custid = 'c000001'
Run it manually and see if it works. The error indicates this is invalid
database or table, so you have to check if they really exist on your server.
HTH,
Plamen Ratchev
http://www.SQLStudio.com
Jack - 22 Jul 2008 17:48 GMT
Thanks a lot for sending me the query output Plamen. I found out the database
name was incorrect. After correcting that everything works fine now. Could
you show me the print statment that gives the generation of sql. Can the
print be used to generate the sql out of stored procedure also. Regards.
> Your code generates the following query:
>
[quoted text clipped - 8 lines]
> Plamen Ratchev
> http://www.SQLStudio.com
Plamen Ratchev - 22 Jul 2008 18:01 GMT
I simply replaced the EXEC statement with PRINT and added the correct
parameter value:
PRINT @sql
I am not sure I understand what you mean to generate SQL out of the stored
procedure. You can return the @sql variable as output parameter if needed.
Or you can use SELECT @sql, not sure what you needs are.
HTH,
Plamen Ratchev
http://www.SQLStudio.com
Jack - 22 Jul 2008 18:15 GMT
Thanks Plamen. That was very helpful.
> I simply replaced the EXEC statement with PRINT and added the correct
> parameter value:
[quoted text clipped - 9 lines]
> Plamen Ratchev
> http://www.SQLStudio.com