我已尝试过以下代码的多种不同组合,但我不断收到同样的错误。
我之前发现的解决方案such as this one,没有嵌入动态SQL的额外复杂性。任何帮助将不胜感激。
代码:
DECLARE @table as NVARCHAR(25) = 'proposals'
DECLARE @column as NVARCHAR(25) = 'daypart'
DECLARE @modulo as NVARCHAR(5) = ''%''
DECLARE @COL_SELECT AS NVARCHAR(2000)
SET @COL_SELECT = 'SELECT DISTINCT column_name
FROM information_schema.COLUMNS
WHERE table_name = ' + @table + '
AND column_name like ' + @modulo + @column + @modulo + ''
EXEC(@COL_SELECT)
错误:
Msg 402,Level 16,State 1,Line 6
数据类型varchar和varchar在模运算符中不兼容。
答案 0 :(得分:1)
错误发生在这里:
DECLARE @modulo as NVARCHAR(5) = ''%'';
这被解释为查找两个空字符串的模数值
在这里使用单引号......
还有一点:您必须为表格的名称添加加倍的引号,并在的LIKE
中添加以创建正确的声明...
DECLARE @COL_SELECT AS NVARCHAR(2000)
SET @COL_SELECT = '
SELECT DISTINCT column_name
FROM information_schema.COLUMNS
WHERE table_name = ''' + @table + '''
AND column_name like ''' + @modulo + @column + @modulo + ''''
答案 1 :(得分:1)
这样的事情应该运行良好:
my %h_tmp = $hash_ref;
打印会给你:
my %h_tmp = ();
此查询应该没有错误地执行。
答案 2 :(得分:1)
威廉
在@table和@modulo变量之前和之后的@COL_SELECT中使用两个引号:
DECLARE @table as NVARCHAR(25) = 'proposals'
DECLARE @column as NVARCHAR(25) = 'daypart'
DECLARE @modulo as NVARCHAR(5) = '%'
DECLARE @COL_SELECT AS NVARCHAR(2000)
SET @COL_SELECT = '
SELECT DISTINCT column_name
FROM information_schema.COLUMNS
WHERE table_name = ''' + @table + '''
AND column_name like ''' + @modulo + @column + @modulo + ''''
EXEC(@COL_SELECT)
希望它有所帮助。 里卡多