I read this post about putting column name and table name as variables. I want square brackets wrapping column name and table name. I saw some people do this:
@sql = 'SELECT [' + @column_name + '] FROM ...'
I tried to put square brackets into the variable like:
CREATE PROCEDURE [dbo].[find_most_frequent]
@table_in VARCHAR,
@col_2 VARCHAR
AS
BEGIN
DECLARE @sql NVARCHAR(4000);
SET @sql =
--start of code
'SELECT' +
@col_2 +
' FROM ' + @table_in +
' GO'
--end of code
print @sql
EXEC SP_EXECUTESQL @sql
END
GO
EXEC [dbo].[find_most_frequent]
@table_in = '[[]dbo].[[]t1]'
,@col_2 = '[[]c1]'
GO
I have used [[] to escape square brackets, as discussed here. But it does work, @sql is
SELECT[ FROM [ GO
Can somebody help please? Thanks!