Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Getting a @Tables @FieldName and @FieldValue into a cursor


Getting a @Tables @FieldName and @FieldValue into a cursor

Author
Message
cdb363
cdb363
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 45
Hello,

I require a SQL statement such as the following, this one works but takes 18 or more seconds to complete. It gets held up on the EXECUTE statement. Ideally, If the cursor could somehow get the field value also I wouldn't need the execute statement. the SP --GetFieldValue below didn't have any performance gains either.

Any ideas or thoughts would be greatly appreciated, if you need more info let me know



ALTER PROCEDURE [dbo].[spCreateNewRecordHistory]
( @EntityID INT,
@RecordID INT,
@WhoCreated nvarchar(100)
)
AS
BEGIN
SET NOCOUNT ON
DECLARE @FieldName nvarchar(100)
DECLARE @Value nvarchar(max)
DECLARE @Object nvarchar(max)
DECLARE @SQL nvarchar(4000)
DECLARE @ParmDefinition nvarchar(100)
DECLARE @EntityName varchar(100)
DECLARE @EntityViewName varchar(100)
DECLARE @Change varchar(200)
DECLARE @Version INT
DECLARE @Result nvarchar(max)

SELECT @EntityName = Name, @EntityViewName = BaseView FROM Entity WHERE ID = @EntityID

DECLARE crObject CURSOR FAST_FORWARD For
SELECT ' FROM INFORMATION_SCHEMA.COLUMNS
WHERE (TABLE_NAME = @EntityViewName) AND Column_name <> 'ID'
OPEN crObject

FETCH NEXT FROM crObject
INTO
@Object ,
@FieldName

-- SET @ParmDefinition = N'@pFieldName nvarchar(100), @pEntityViewName nvarchar(100), @ValueOUT nvarchar(200) OUTPUT'

SET @ParmDefinition = N'@pFieldName nvarchar(100), @pEntityViewName nvarchar(100), @ValueOUT nvarchar(200) OUTPUT'

SET @Result = ''

WHILE @@FETCH_STATUS = 0
BEGIN
--SET @SQL = N'SELECT @ValueOUT = @pFieldName FROM '+ @EntityViewName +' WHERE ID='+CAST(@RecordID as varchar)
SET @SQL = N'SELECT @ValueOUT = '+@FieldName+' FROM '+ @EntityViewName +' WHERE ID='+CAST(@RecordID as varchar)

EXECUTE sp_executesql @SQL, @ParmDefinition, @pFieldName = @FieldName, @pEntityViewName = @EntityViewName, @ValueOUT=@Value OUTPUT

--EXEC dbo.spGetFieldValue @EntityViewName, @FieldName, @RecordID, @Value OUTPUT

IF ISNULL(@Value,'') <> ''
SET @Result = @Result + @Object+'>' + @Value +''
ELSE
SET @Result = @Result + @Object + ' />'

FETCH NEXT FROM crObject
INTO
@Object ,
@FieldName

END -- WHILE

SET @Result = @Result + '
'
SET @Change = ''
INSERT INTO EntityRecordVersions (EntityID ,RecordID, Changes, Comments, RecordData, DateUpdated, WhoUpdated, Type)
VALUES (@EntityID ,@RecordID ,@Change ,NULL ,@Result ,GETDATE() ,SUSER_NAME(),1)

CLOSE crObject
DEALLOCATE crObject
END
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search