遇到UPDATE命令问题

时间:2011-08-02 12:44:26

标签: sql

使用此SP我在UPDATE语句中不断收到错误,表明我有转换问题或其他问题。但ID在我的所有表中都是一个整数....

转换varchar值时,转换失败'UPDATE tblLangenUS SET [lang_String] =' - asdfgsdfg',[prev_LangString] ='',[needsTranslation] ='false'WHERE [ID] =''数据类型为int。

    @currTable varchar(100),
    @ID int,
    @short_Text varchar(250),
    @brief_Descrip varchar(250) = Null,
    @needsTranslation varchar(10) = Null,
    @prev_LangString varchar(250) = Null,
    @lang_String varchar(250) = Null,
    @original_lang_String varchar(250) = Null,
    @StringID_from_Master varchar(250),     
    @GUID varchar(250) = Null

/*

* /

AS        
SET NOCOUNT ON;

DECLARE @userTable AS VARCHAR(200);
SET @userTable = @currTable
DECLARE @submitDate1 DATETIME;
SET @submitDate1 = GETDATE()



SET @prev_LangString = @original_lang_String
SET @needsTranslation = 'false'

DECLARE @sql varchar(max)
    -- Establish update to the language tabel of user and prepare to search DB for all strings that will need to be updated.
BEGIN

   SET @sql = 'UPDATE ' + @currTable + ' SET [lang_String] = ''' + @lang_String + ''', [prev_LangString] = ''' + @prev_LangString + ''', [needsTranslation] = ''' + @needsTranslation + ''' WHERE [ID] = ''' + @ID + '''; ' 
   EXEC @sql
   @sql = ''



END

BEGIN               
    DECLARE usedIN_DBScursor CURSOR
    FOR
    SELECT       tblUniquetblStringsMaster_ID, Database_Name, dbKeyID_ofStringName
    FROM         tblDBUsage
    WHERE        (tblUniquetblStringsMaster_ID = @StringID_from_Master );


                -- Declare the variables to store the values returned by FETCH.
    DECLARE @tblUniquetblStringsMaster_ID AS INT;
    DECLARE @dbKEYID as INT;
    DECLARE @dbName as varchar(100);

    OPEN usedIN_DBScursor;

    -- Perform the first fetch and store the values in variables.
    -- Note: The variables are in the same order as the columns
    -- in the SELECT statement. 

    FETCH NEXT FROM usedIN_DBScursor
    INTO @tblUniquetblStringsMaster_ID, @dbName, @dbKEYID;

    -- Check @@FETCH_STATUS to see if there are any more rows to fetch.
    WHILE @@FETCH_STATUS = 0
    BEGIN
    -- Update pending strings table with translation.           
        BEGIN
            INSERT INTO tblPendingDBUpdates
                                     (stringMasterID, databaseName, databaseStringID, englishText, foreignLangText, submitDate, GUID)
            VALUES        (@StringID_from_Master, @dbName, @dbKEYID, @short_Text, @lang_String, @submitDate1, @GUID);                                       
        END 
--      SET @sql = ''
    -- This is executed as long as the previous fetch succeeds.
        FETCH NEXT FROM usedIN_DBScursor
        INTO @tblUniquetblStringsMaster_ID, @dbName, @dbKEYID;
    END

    CLOSE usedIN_DBScursor;
    DEALLOCATE usedIN_DBScursor;    

END 



RETURN

1 个答案:

答案 0 :(得分:3)

如果ID为int,则必须CAST将其设为nvarchar

SET @sql = 'UPDATE ' + @currTable + ' SET [lang_String] = ''' + @lang_String + ''', [prev_LangString] = ''' + @prev_LangString + ''', [needsTranslation] = ''' + @needsTranslation + ''' WHERE [ID] = ''' + CAST(@ID as nvarchar(10)) + '''; '

否则,您尝试将字符串和整数一起添加,这将无效。

请注意这一部分:

   ' WHERE [ID] = ''' + CAST(@ID as nvarchar(10)) + '''

我认为您还必须将EXEC语法更改为:

EXEC(@sql)