i have parameters for call table name on my database, for example table name in my db like GPS-RV.1_b. then i try to call it in my query builder like
declare @cmd nvarchar(max)
set @cmd = ' select top 100 X,Y,Z from dbo.' + @tableName
DECLARE @ParmDefinition nvarchar(100) = N'@tableName nvarchar(100)';
print @cmd
exec sp_executesql @cmd, @ParmDefinition = '@tableName';
it's run, but when i try to enter my table name and press ok button, it showing this error
------------------------------ An error occurred while executing the query. Procedure expects parameter '@parameters' of type 'ntext/nchar/nvarchar'. ------------------------------ ADDITIONAL INFORMATION: Procedure expects parameter '@parameters' of type 'ntext/nchar/nvarchar'. (Microsoft SQL Server, Error: 214) For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=10.50.1617&EvtSrc=MSSQLServer&EvtID=214&LinkId=20476 ------------------------------ BUTTONS: OK ------------------------------
is there any missing part from my query?? or i do wrong way?? thanks for help and any advice to fix it.
Just exec your @cmd
statement without sent parameters
declare @cmd nvarchar(max)
set @cmd = ' select top 100 X,Y,Z from dbo.' + @tableName
print @cmd
exec sp_executesql @cmd
As @Mitch Whaeat said, @tableName
is not a parameter, It's just a variable. Remember to assign the value to the @tableName
variable.