Having issues with UPDATE command
Using this SP I keep getting an error on the UPDATE statement indicating that I have conversion problem or something. But ID is an integer in all of my tables....
Conversion failed when converting the varchar valu开发者_开发技巧e 'UPDATE tblLangenUS SET [lang_String] = ' - asdfgsdfg', [prev_LangString] = ' ', [needsTranslation] = 'false' WHERE [ID] = '' to data type 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
If the ID is an int
, you must CAST
it to an nvarchar
SET @sql = 'UPDATE ' + @currTable + ' SET [lang_String] = ''' + @lang_String + ''', [prev_LangString] = ''' + @prev_LangString + ''', [needsTranslation] = ''' + @needsTranslation + ''' WHERE [ID] = ''' + CAST(@ID as nvarchar(10)) + '''; '
Otherwise, you are trying to add a string and an integer together, which won't work.
Notice this part:
' WHERE [ID] = ''' + CAST(@ID as nvarchar(10)) + '''
I think you must also change your EXEC syntax to:
EXEC(@sql)
精彩评论