Hi
You are missing the database name in your 4 part name and you can not use a
variable to define the server name try:
ALTER PROCEDURE [sp_dest_comp]
@dest_comp varchar(20),
@product_code varchar(4) Output
AS
Declare @stmt nvarchar(120)
Declare @params nvarchar(100)
Set
@stmt = N'Select Top 1 * From ['
+ @dest_comp
+ N'].Northwind.dbo.products '
+ N'Where ProductID = '
+ N'@product_code'
Set @params = N'@dest_comp varchar(20), '
+ N'@product_code varchar(4)'
Execute sp_executesql
@stmt,
@params = @params,
@dest_comp = @dest_comp,
@product_code = @product_code
GO
John
[quoted text, click to view] "Elizabeth" wrote:
> Hi there,
> I am trying to execute sp_executesql while producing the database name
> dynamically.
> The code look something like that:
>
> CREATE PROCEDURE [sp_dest_comp]
> @dest_comp varchar(20),
> @product_code varchar(4) Output
> AS
>
> Declare @stmt nvarchar(120)
> Declare @params nvarchar(100)
>
> Set
> @stmt = N'Select Top 1 * From '
> + N'@dest_comp'
> + N'.dbo.products '
> + N'Where product_code = '
> + N'@product_code'
>
> Set
> @params = N'@dest_comp varchar(20), '
> + N'@product_code varchar(4)'
>
> Execute sp_executesql
> @stmt,
> @params = @params,
> @dest_comp = @dest_comp,
> @product_code = @product_code
> GO
>
> I get an error saying:
> Line 1: Incorrect syntax near '.'.
>
> What's wrong?
>
> Many thanks in advance
> --