0

I have searched high and low, without finding an answer. So I hope that you guys n girls can help me on my way:

I cant figure out why @old_comment is NULL when I use it in "SET @new_comment...", but it returns a fine value when I use it in the outputparameter "SET @commentOldOUT..."

    CREATE PROCEDURE [dbo].[SP_NY_KOMMENTAR]
    @tabel NVARCHAR(100),
    @id INT,
    @comment NVARCHAR(1000) = NULL,
    @commentOldOUT NVARCHAR(1000) = NULL OUTPUT
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @user NVARCHAR(30);
    DECLARE @date NVARCHAR(10);
    DECLARE @new_id VARCHAR(100);
    DECLARE @new_comment NVARCHAR(MAX);
    DECLARE @old_comment NVARCHAR(MAX);
    DECLARE @old_comm NVARCHAR(MAX);
    DECLARE @old NVARCHAR(MAX);
    DECLARE @q2 NVARCHAR(MAX);

    SET @new_id = (SELECT CAST(@id AS VARCHAR(100)));
    SET @user = (SELECT ORIGINAL_LOGIN());
    SET @date = (SELECT CONVERT(DATE,GETDATE()));
    SET @old = 'SELECT comment FROM '+ @tabel +' WHERE id = ' + @ny_id;

    EXEC sp_executesql 
        @query = @old, 
        @params = N'@old_comm NVARCHAR(MAX) OUTPUT', 
        @old_com =  @old_comm OUTPUT;

    SET @old_comment = (SELECT @old_comm);

    SET @commentOldOUT = @old_comment;                          

    SET @new_comment = COALESCE(@old_comment + CHAR(13),'') + '[' + @user + ' ' + @date + '] ' + @comment;

    SET @q2 = N'UPDATE ' + @tabel + ' SET comment = ''' + @new_comment + ''' WHERE id = ' + @ny_id;
    EXEC (@q2);
END
4

3 回答 3

0

Sorry, I tried your query but for me it works fine! Have you tried to "debug" your query? You can see the value of every variable when in debug. (The green arrow in SQL Server 2008)

于 2012-12-18T15:42:35.010 回答
0

I'm guessing that you didn't pass a value explicitly for the @comment parameter? If so, the problem would be here:

SET @new_comment = COALESCE(@old_comment + CHAR(13),'') + '[' + @user + ' ' + @date + '] ' + @comment;

The parameter @comment has a default value of NULL and concatenating anything with NULL results in NULL. That would explain why @old_comment, @user and @date all have values, but when it put it all together then @new_comment is NULL. This would be a better default value:

@comment NVARCHAR(1000) = '',

And your code as written is a bit messy, I assume that @old_comment and @commentOldOUT are only for debugging purposes? I don't see why you don't use @old_comm directly.

Finally, if you're working with dynamic SQL then I strongly recommend adding a debugging parameter to print out your SQL strings easily. It's a very useful troubleshooting tool, especially if you don't have SSMS available.

于 2012-12-19T15:23:22.893 回答
0

Well I can now see that I did several things wrong, below is the new, and now working, stored procedure:

    CREATE PROCEDURE [dbo].[SP_NY_KOMMENTAR]
    @tabel NVARCHAR(100),
    @id INT,
    @comment NVARCHAR(1000) = NULL
AS
IF @comment IS NOT NULL 
    BEGIN
        SET NOCOUNT ON;
        DECLARE @user NVARCHAR(30);
        DECLARE @date NVARCHAR(10);
        DECLARE @SQLSelect nvarchar(500);
        DECLARE @ParmSelect nvarchar(500);
        DECLARE @old_comment NVARCHAR(MAX);
        DECLARE @old_commentOUT NVARCHAR(MAX);
        DECLARE @new_comment NVARCHAR(MAX);
        DECLARE @SQLUpdate nvarchar(500);
        DECLARE @ParmUpdate nvarchar(500);

        SET @user = (SELECT ORIGINAL_LOGIN());
        SET @date = (SELECT CONVERT(DATE,GETDATE()));
        SET @SQLSelect = N'SELECT @old_commentOUT = kommentar FROM ' + QUOTENAME(@tabel) + ' WHERE id = @id';
        SET @ParmSelect = N'@id INT, @old_commentOUT NVARCHAR(MAX) OUTPUT';

        EXEC sp_executesql 
            @SQLSelect,
            @ParmSelect,
            @id = @id,
            @old_commentOUT = @old_comment OUTPUT;

        SET @old_comment = (SELECT @old_comment);
        SET @new_comment = COALESCE(@old_comment + CHAR(13) + CHAR(10),'') + '[' + @user + ' ' + @date + '] ' + @comment;

        SET @SQLUpdate = N'UPDATE ' + QUOTENAME(@tabel) + ' SET kommentar = @new_comment WHERE id = @id';
        SET @ParmUpdate = N'@id INT, @new_comment NVARCHAR(MAX)';
        EXEC sp_executesql 
            @SQLUpdate, 
            @ParmUpdate,
            @id = @id,
            @new_comment = @new_comment;
    END

For example I weren't using parameterbinding correct, after that I tried to bind tabelnames as a parameter...

于 2012-12-20T10:30:36.243 回答