SQL Server:尝试使用变量语句进行查询时出现EXECUTE错误

时间:2018-02-02 11:14:36

标签: sql-server tsql exec

任务是滚动所有表和列以查找包含搜索值的表和列名称。我使用的脚本是:

namespace CreateEntityConsole
{
    class Entity
    {
        string domain = "DESKTOP-I4VK2LV";
        string userName = "AP-502";
        string password = "pass";
        string appID = "bbb";
        string locale = "en-US";
        string contenttype = string.Empty;

        // Create ENTITY
        public string CreateEntity()
        {
            string URI = "http://localhost:13490/agilepointserver/extension/createentity";

            string JsonRequestData = "{\"EntityName\":[\"AccountContact\":[\"PropertiesJSON\":[\"AName\": \"chaitratest2\",\"region\": \"India\"]]]}";

            HttpOperations ops = new HttpOperations(domain, this.userName, password, appID, locale);
            // HttpOperations ops = new HttpOperations(this.userName, password, appID, locale);

            return ops.InsertEntity(URI, JsonRequestData);
        }

        public void InsertIntoDB(string JsonRequestData)
        {
            using (SqlConnection sqlCon = new SqlConnection())
            {
                sqlCon.ConnectionString = "server=DESKTOP-I4VK2LV;Integrated Security=True;database=Entity";
                sqlCon.Open();
            }
        }
    }
}

执行此操作时,出现错误:

  

关键字' EXEC'

附近的语法不正确

请帮我制作此代码

2 个答案:

答案 0 :(得分:1)

这条线存在一些问题......

  IF @Search_Value = Exec('select distinct' + @Column_Name + 'from' 
                          + @Table_Name + 'where' + @Column_Name + '=' 
                          + @Search_Value)


首先,确保在sql字符串的必要位置有空格......

Exec('select distinct ' + @Column_Name + ' from ' 
                        + @Table_Name + ' where ' + @Column_Name + ' = ' 
                        + @Search_Value
)


接下来,如果执行SELECT语句,则结果是数据集,而不是标量。这也意味着数据集也不会返回,因为函数返回标量结果。

可以使用EXEC @myReturn = spSomethingOrAnother(@param, @anotherParam);来捕获使用RETURN语句(在SP的结尾处)发回的任何内容,但我认为这不起作用使用动态SQL ...

https://docs.microsoft.com/en-us/sql/t-sql/language-elements/execute-transact-sql


您的下一个选项可能是创建一个表格以将结果插入到,然后检查该表...

INSERT INTO @TABLE EXEC @query with SQL Server 2000

CREATE TABLE #result (search_value UNIQUEIDENTIFIER)

INSERT INTO 
  #result (
    search_value
  )
EXEC(
  'select distinct ' + @Column_Name + ' from ' 
                     + @Table_Name + ' where ' + @Column_Name + ' = ' 
                     + @Search_Value
)

IF EXISTS (SELECT * FROM #result WHERE search_value = @search_value)

...

答案 1 :(得分:0)

{USE PROD
GO
IF OBJECT_ID('tempdb..#source_table') IS NOT NULL DROP TABLE #source_table 
GO
IF OBJECT_ID('tempdb..#result_table') IS NOT NULL DROP TABLE #result_table 
GO
CREATE TABLE #result_table (Table_Name nvarchar(max),Column_Name nvarchar(max),Searched_Value uniqueidentifier)
DECLARE @CURSOR             CURSOR
DECLARE @Table_Name         nvarchar(max)
DECLARE @Column_Name        nvarchar(max)
DECLARE @Search_Value       nvarchar(max) = concat('''','ABBDFFEA-4576-4AA9-854E-A016433C54F0','''')
SET @CURSOR  = CURSOR SCROLL
    FOR
    (
        select s.TABLE_NAME, s.COLUMN_NAME 
        from INFORMATION_SCHEMA.COLUMNS s
        where s.DATA_TYPE = 'uniqueidentifier'
    )
OPEN @CURSOR
FETCH NEXT FROM @CURSOR INTO @Table_Name, @Column_Name
WHILE @@FETCH_STATUS = 0  
BEGIN
INSERT INTO #result_table (Table_Name, Column_Name, Searched_Value) EXEC('select distinct ' + ''''+ @Table_Name + '''' + ' AS Table_Name , ' + ''''+ @Column_Name + '''' + ' AS Column_Name , ' + '[' + @Column_Name + ']' + ' from ' + '[' + @Table_Name + ']' + ' where ' + '[' + @Column_Name + ']' + ' = ' + @Search_Value)
FETCH NEXT FROM @CURSOR INTO @Table_Name, @Column_Name
END
CLOSE @CURSOR
--results
SELECT * FROM #result_table}