Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

Updating SQL Table Records + Dynamic SQL OR Dynamic Cursor

$
0
0
All,I have been working on this project for a while and seem to have hit a roadblock. I have a two stored procedure.The first decides the table name and passes this to the second stored procedure as a parameterThe second procedure needs to use that table name parameter and loop through the records in the table and make updates.Below is a sample using a cursor which did not work? Any suggestions on how I can make this work:[quote]ALTER PROCEDURE [dbo].[usrSetLTDNormDist] -- Add the parameters for the stored procedure here @Symbol nvarchar(50), @LTDMean float, @LTDStdev float --<@Param2, sysname, @p2> <Datatype_For_Param2, , int> = <Default_Value_For_Param2, , 0>ASBEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; DECLARE @TableName nvarchar(60) DECLARE @Volume bigint; DECLARE @NormDist float; Declare @TransDate date; SET @TableName = 'VNAS'+ REPLACE(@Symbol, '-', '_') DECLARE @SQLCommand1 NVARCHAR(MAX) SET @SQLCommand1 = replace(N' Declare @RecCursor1 Cursor For Select [Volume], [TRANSDATE] from <@TableName>' ,'<@TableName>', @TableName) EXECUTE dbo.sp_executesql @sqlCommand1 -- Open Cursor **** DOES NOT WORK **** CURSOR NOT SET **** Open @RecCursor1 Fetch Next From @RecCursor1 Into @Volume, @TransDate While @@FETCH_STATUS = 0 -- Begin Logic for slected symbol Begin -- Do Stuff ****** Fetch Next From @RecCursor1 Into @Volume, @TransDate End; Close @RecCursor1 Deallocate @RecCursor1END[/quote]

Viewing all articles
Browse latest Browse all 3145

Trending Articles