简体   繁体   中英

SQL: Dynamic SQL issue

I'm not sure why this is not working. I have it set up to Return a result of 1 if its successfully saving but I keep getting a 0. This was working but I recently added difficulty to the query. Now its not working:

SQL Query:

DECLARE
    @Email AS NVARCHAR(500) = 'uokgames@gmail.com',
    @Update AS NVARCHAR(MAX) = 'SET Tile_332 = 1,Tile_216 = 1,Tile_296 = 1,Tile_303 = 1,Tile_313 = 1,Tile_274 = 1,Tile_261 = 1,Tile_311 = 1,Tile_320 = 1,Tile_204 = 1,Tile_304 = 1,Tile_187 = 1,Tile_291 = 1',
    @Board AS NVARCHAR(100) = 'Templo_Mayor',
    @Difficulty AS INT = 0
--AS
BEGIN
    DECLARE @sql AS NVARCHAR(MAX),
            @Response AS BIT  = 0

    BEGIN TRY
        SET @sql = 'UPDATE [dbo].[Game_Board] ' +  @Update + ' WHERE [Email] =''' + @Email + ''' AND [Board_Name] = ''' + @Board + ''' AND [Difficulty] = ' + @Difficulty
        EXECUTE sp_executesql @sql  

        SET @Response = 1;
    END TRY
    BEGIN CATCH
        SET @Response = 0;
    END CATCH

    SELECT @Response
END 

Here you just need to convert difficulty to varchar(1) and it will work.

DECLARE
@Email AS NVARCHAR(500) = 'uokgames@gmail.com',
@Update AS NVARCHAR(MAX) = 'SET Tile_332 = 1,Tile_216 = 1,Tile_296 = 1,Tile_303 = 1,Tile_313 = 1,Tile_274 = 1,Tile_261 = 1,Tile_311 = 1,Tile_320 = 1,Tile_204 = 1,Tile_304 = 1,Tile_187 = 1,Tile_291 = 1',
@Board AS NVARCHAR(100) = 'Templo_Mayor',
@Difficulty AS varchar(1) = '0'
--AS
BEGIN
    DECLARE @sql AS NVARCHAR(MAX),
            @Response AS BIT  = 0

    BEGIN TRY
        SET @sql = 'UPDATE [dbo].[Game_Board] ' +  @Update + ' WHERE [Email] =''' + @Email + ''' AND [Board_Name] = ''' + @Board + ''' AND [Difficulty] = ' + @Difficulty
        EXECUTE sp_executesql @sql  

        SET @Response = 1;
    END TRY
    BEGIN CATCH
        SET @Response = 0;
    END CATCH

    SELECT @Response
END 

You need to remove the try catch thing so you will get the real error:

Msg 245, Level 16, State 1, Line 12
Conversion failed when converting the nvarchar value 'UPDATE [dbo].[Game_Board] SET Tile_332 = 1,Tile_216 = 1,Tile_296 = 1,Tile_303 = 1,Tile_313 = 1,Tile_274 = 1,Tile_261 = 1,Tile_311 = 1,Tile_320 = 1,Tile_204 = 1,Tile_304 = 1,Tile_187 = 1,Tile_291 = 1 WHERE [Email] ='uokgames@gmail.com' AND [Board_Name] = 'Templo_Mayor' AND [Difficulty] = ' to data type int.

Use parameters!

BEGIN TRY
    SET @sql = 'UPDATE [dbo].[Game_Board] ' +  @Update + ' WHERE [Email] = @Email AND [Board_Name] = @Board AND [Difficulty] = @Difficulty';

    EXECUTE sp_executesql @sql,
            N'@Email NVARCHAR(500), @Board NVARCHAR(100), @Difficulty INT,
            @Email=@Email, @Board=@Board, @Difficulty=@Difficulty  

    SET @Response = 1;
END TRY
BEGIN CATCH
    SET @Response = 0;
END CATCH

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM