Here goes, second post, first didn’t go to well….
I am calling an SP from a Gridview in ASP. I pass in a table name as a variable along with some other variables. I need to UPDATE my original table that the Gridview attaches to but also build a Datalist, I have used a Fetch and got it working fine. Cycle through those records and INSERT data into third table. I was told (in first post) I need to build the SQL string first then execute it. when I write it that way the second part of the insert does not work.
Here is the code in its sliced up form because of efforts to find a succesfull structure….
@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
-- DECLARE @sql nvarchar(4000)
SELECT @sql = ' UPDATE ' + @currTable +
' SET [lang_String] = ' + @lang_String +
' WHERE (ID = ' + @ID + ' ';
EXEC sp_executesql @sql, N'@ID nvarchar(10)', @ID
-- UPDATE @userTable
-- SET [lang_String] = @lang_String, [date_Changed] = @submitDate1, [prev_LangString] = @prev_LangString, [needsTranslation] = @needsTranslation, [brief_Descrip] = @brief_Descrip
-- WHERE (ID = @ID)
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
It seems that your procedure could be re-written as follows:
However, I am not sure why you need a cursor in your original version, or what you mean by “does not work.” Can you explain?