Is there something wrong with my code? I get an exception:
Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
I'm trying to update a dynamic table with dynamic columns depending on the searched facility of user
ALTER PROCEDURE SP_UPDATE_FACILITY
@featid int,
@facilityid int,
@updatetbl as TABLE_UPDATE_FACILITYDETAILS READONLY
AS
BEGIN
SET NOCOUNT ON;
DECLARE @tblname AS NVARCHAR(255);
SET @tblname = (SELECT dbo.FNC_Search_GetSearchTable(@facilityid));
DECLARE @key varchar(255);
DECLARE @value varchar(255);
DECLARE @MyCursor CURSOR
SET @MyCursor = CURSOR FOR
SELECT col_key, col_value FROM @updatetbl
OPEN @MyCursor
FETCH NEXT FROM @MyCursor INTO @key , @value
WHILE @@FETCH_STATUS = 0
BEGIN
IF(@key != '' and @value != '')
BEGIN
BEGIN TRY
SET @value = (CAST ( @value AS varchar(255)));
END TRY
BEGIN CATCH
SET @value = (CAST ( @value AS float));
END CATCH;
DECLARE @sSQL NVARCHAR(500);
SET @sSQL = 'UPDATE ' + QUOTENAME(@tblname) + ' SET ' + QUOTENAME(@key) + ' = @value WHERE FEATID = @featid'
EXEC sp_executesql @sSQL , N'@value VARCHAR(255), @featid INT', @key, @featid
FETCH NEXT FROM @MyCursor INTO @key, @value
END
END;
CLOSE @MyCursor ;
DEALLOCATE @MyCursor;
END
GO
Is there a way to loop through rows of table then get the values per rows of table
sp_prefix for your stored procedures. Microsoft has reserved that prefix for its own use (see Naming Stored Procedures), and you do run the risk of a name clash sometime in the future. It's also bad for your stored procedure performance. It's best to just simply avoidsp_and use something else as a prefix - or no prefix at all!