Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用动态模式TSQL SSMS执行插入_Sql_Sql Server_Tsql_Ssms - Fatal编程技术网

使用动态模式TSQL SSMS执行插入

使用动态模式TSQL SSMS执行插入,sql,sql-server,tsql,ssms,Sql,Sql Server,Tsql,Ssms,我知道这有点多,但我从4天开始就在寻找解决方案。我只是想执行一个INSERT,但我总是在ssm中收到一些神秘的错误消息 输入变量错误,因此应填写错误日志: USE [Tfs_GitInterface] GO DECLARE @return_value int EXEC @return_value = [dbo].[spInsertChangeLevel2] @repositoryName = N'asdasd', @commitId = 213123,

我知道这有点多,但我从4天开始就在寻找解决方案。我只是想执行一个INSERT,但我总是在ssm中收到一些神秘的错误消息

输入变量错误,因此应填写错误日志:

USE [Tfs_GitInterface]
GO
DECLARE @return_value int
EXEC    @return_value = [dbo].[spInsertChangeLevel2]
        @repositoryName = N'asdasd',
        @commitId = 213123,
        @changeLevel = N'asda'
SELECT  'Return Value' = @return_value
GO
存储过程:

CREATE PROCEDURE [dbo].[spInsertChangeLevel2]
-- Add the parameters for the stored procedure here
@repositoryName [nvarchar](256),
@commitId [binary](20),
@changeLevel [nvarchar](400)
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @repId [int]
    DECLARE @commitIdValid [int];
    DECLARE @errorOccured [BIT] = 0;
    DECLARE @schema [nvarchar](400) = 'dbo';
    DECLARE @schemaPlusTable [nvarchar](400) = @schema + '.tbl_ErrorLog';
    DECLARE @changeErrorMsg [nvarchar](400) = 'Wrong ChangeLevel';
    DECLARE @changeSQL [nvarchar](MAX);

    BEGIN TRY
        IF
        (@changeLevel <> 'NewFeature' AND @changeLevel <> 'Patch' AND @changeLevel <>'BreakingChange')
        BEGIN
            PRINT 'Wrong ChangeLevel';

            EXEC('INSERT INTO [' + @schema + '].[tbl_ErrorLog] ( [repositoryName], [commitId], [changeLevel], [error], [timestamp] )
                  VALUES ( "test" , ' + @commitId + ' , ' + @changeLevel + ' , ' + @changeErrorMsg + ' , SYSDATETIME() )');

            SET @errorOccured = 1;
        END

        IF NOT EXISTS
        (SELECT InternalRepositoryId FROM dbo.tbl_GitRepository WHERE Name = @repositoryName)
        BEGIN
            PRINT 'Repository-Name ist nicht bekannt'
            INSERT INTO [dbo].[tbl_ErrorLog]
                    ([repositoryName]
                    ,[commitId]
                    ,[changeLevel]
                    ,[error]
                    ,[timestamp])
            VALUES 
                    (@repositoryName
                    ,@commitId
                    ,@changeLevel
                    ,'Repository-Name is unknown'
                    ,SYSDATETIME())
            SET @errorOccured = 1;
        END

        SELECT @repId = InternalRepositoryId FROM
        dbo.tbl_GitRepository WHERE
        Name = @repositoryName;

        IF NOT EXISTS
        (SELECT CommitId FROM dbo.tbl_GitCommit WHERE CommitId = @commitId AND InternalRepositoryId = @repId)
        BEGIN
            PRINT 'CommitId is unknown'
            INSERT INTO [dbo].[tbl_ErrorLog]
                    ([repositoryName]
                    ,[commitId]
                    ,[changeLevel]
                    ,[error]
                    ,[timestamp])
            VALUES 
                    (@repositoryName
                    ,@commitId
                    ,@changeLevel
                    ,'CommitId is unknown'
                    ,SYSDATETIME())
            SET @errorOccured = 1;
        END

        IF(@errorOccured = 1) RETURN 0;

        INSERT INTO [dbo].[tbl_ChangeLevel]
              ([InternalRepositoryId]
              ,[CommitId]
              ,[ChangeLevel])
         VALUES
              (@repId
              ,@commitId
              ,@changeLevel)
    END TRY
    BEGIN CATCH
    PRINT 'Msg: ' + ERROR_MESSAGE();
    PRINT  ERROR_LINE();
    PRINT  ERROR_NUMBER();
    PRINT 'Procedure: ' + ERROR_PROCEDURE();
    PRINT  ERROR_STATE();
    RETURN 0;
    END CATCH
    RETURN 1;
END
这实际上很简单,这个存储过程测试这3个变量是否合法/已知,如果是,它会将它们插入tbl_ChangeLevel-table。对于每个错误的变量,its在tbl_ErrorLog表中创建一行,这意味着我们在tbl_ChangeLevel中有一个新行,或者在tbl_ErrorLog中有1-3行

此时,我将ChangeLevel上的第一个错误测试更改为DynamicExec,因此schema@schema可以改变。我在这里什么都试过了。起初我使用sp_executesql和EXECUTE,但没有任何效果,现在当我调试它时,它捕捉到一个错误:

错误的变更级别 Msg:“̀”附近的语法不正确

二,

102

一,

1行受影响


是的,这不是你屏幕上的污垢->“̀”你必须从outlook或其他来源复制粘贴查询的一部分。其他源具有不同的编码格式。 如果您有Notepad++或ultraedit之类的文本编辑器,请将查询编码更改为UTF-8,而不使用BOM表


如果问题仍然存在,我建议在SSMS查询窗口中重写查询

您需要更多的引号才能在实际插入中使用引号。例如,此“++@changeLevel+”应变为“++@changeLevel+”。请尝试打印插入而不是执行插入。还请记住,当前构建字符串的方式是,如果其中一个参数获得NULL值,则整个结果将以NULL结束。注意,您正在使用双引号插入测试。这应该是2个单引号以及测试。