Hi thereI'm having some problems with a parameterized dynamic sql query.I am using ADO.NET to set up the query and use parameters to specify the table name and where clause. The SQL that I can see hitting the server is as follows (line breaks added for readability):declare @P1 intset @P1=NULL exec sp_prepexec @P1 output, N'@DBL varchar(100),@CID int', N'SELECT PTAID, PA_Value FROM @DBL WHERE CID=@CID', @DBL = 'DCMS2_Live.dbo.Data_ProductAttribute', @CID = 1787select @P1
This results in the following error message:Server: Msg 137, Level 15, State 2, Line 1Must declare the variable '@DBL'.Server: Msg 8180, Level 16, State 1, Procedure sp_prepexec, Line 3Statement(s) could not be prepared.(1 row(s) affected)
If it's any help, the c# code is as follows:string strSqlCmd; DataSet ds = new DataSet(), lds = new DataSet(); SqlCommand sqlCmd = new SqlCommand(); SqlDataAdapter mySqlDataAdapter; strSqlCmd="SELECT PTAID, PA_Value FROM @DBL WHERE CID=@CID"; sqlCmd = new SqlCommand(strSqlCmd, connDBConnection, tran); sqlCmd.CommandType=CommandType.Text; sqlCmd.Parameters.Add("@DBL", SqlDbType.VarChar, 100); sqlCmd.Parameters["@DBL"].Value=strDBLiveName+".dbo.Data_ProductAttribute"; sqlCmd.Parameters.Add("@CID",SqlDbType.Int).Value=CID; sqlCmd.Prepare(); mySqlDataAdapter = new SqlDataAdapter(sqlCmd); mySqlDataAdapter.Fill(lds);
I'm really tearing my hair out on this one and any pointers as to where I am going wrong would be very much appreciated!Thanks!