Quantcast
Channel: Forum Getting started with SQL Server
Viewing all articles
Browse latest Browse all 7129

Insert in to table using Cursors ?

$
0
0

Hi,

I have more than 300000 records ( more than 11 GB ) with content datatype in one table called "table1 ( server 1)"

I would like to insert all data from "table1" to "table2 (server 2 )"

If i try using SQL Import/Export and below method also giving errors ..

WITH Attachments AS

		(
			SELECT  [refid],[filename],[FileCategory],[Description],[Content],[emailrefid]
		,[Status],[DateUpdated],[UpdatedBy],[FileMainCategory]
		,[InitiatorName],[MainClassofInsurance],[PlacementNo],[AddendumNo]
		,[ClaimNo],[EndorsementNo],[OtherRefNo],[GroupName],[PolicyYear]
			FROM  [TaskManagement].[dbo].[t_Attachments]
		) 
		INSERT INTO [dbo].[t_Attachments]
		([refid],[filename]    ,[FileCategory],[Description],[Content],[emailrefid]
		,[Status],[DateUpdated],[UpdatedBy],[FileMainCategory],[InitiatorName],[MainClassofInsurance]
		,[PlacementNo],[AddendumNo],[ClaimNo],[EndorsementNo]
		,[OtherRefNo],[GroupName]
		,[PolicyYear])	 
		SELECT  [refid],[filename],[FileCategory],[Description],[Content],[emailrefid]
		,[Status],[DateUpdated],[UpdatedBy],[FileMainCategory]
		,[InitiatorName],[MainClassofInsurance],[PlacementNo],[AddendumNo]
		,[ClaimNo],[EndorsementNo],[OtherRefNo],[GroupName],[PolicyYear]
			FROM Attachments  

So then I am trying create a stored proc and  I am not getting how to write this .

Every loop I would like to insert 100 records , but how to do ?

this cursor will loop 300000 times and it's wrong ..

	    --TRUNCATE TABLE [BIBKUL5].[TaskManagement5].[dbo].t_Attachments
		DECLARE users_cursor CURSOR
		FOR
		SELECT Count(*) FROM  [BIBKUL8].[TaskManagement].[dbo].[t_Attachments]

		OPEN users_cursor

		FETCH NEXT
		FROM users_cursor
		INTO @RowCount

		WHILE @@FETCH_STATUS = 0
		BEGIN
	 
		WITH Attachments AS

		(
			SELECT  ROW_NUMBER() OVER (ORDER BY id) AS RowNumber,[refid],[filename],[FileCategory],[Description],[Content],[emailrefid]
		,[Status],[DateUpdated],[UpdatedBy],[FileMainCategory]
		,[InitiatorName],[MainClassofInsurance],[PlacementNo],[AddendumNo]
		,[ClaimNo],[EndorsementNo],[OtherRefNo],[GroupName],[PolicyYear]
			FROM [TaskManagement].[dbo].[t_Attachments]
		) 
		INSERT INTO [dbo].[t_Attachments]
		([refid],[filename]    ,[FileCategory],[Description],[Content],[emailrefid]
		,[Status],[DateUpdated],[UpdatedBy],[FileMainCategory],[InitiatorName],[MainClassofInsurance]
		,[PlacementNo],[AddendumNo],[ClaimNo],[EndorsementNo]
		,[OtherRefNo],[GroupName]
		,[PolicyYear])	 
		SELECT  [refid],[filename],[FileCategory],[Description],[Content],[emailrefid]
		,[Status],[DateUpdated],[UpdatedBy],[FileMainCategory]
		,[InitiatorName],[MainClassofInsurance],[PlacementNo],[AddendumNo]
		,[ClaimNo],[EndorsementNo],[OtherRefNo],[GroupName],[PolicyYear]
			FROM Attachments WHERE RowNumber BETWEEN 1 AND 100
			 

			 
			FETCH NEXT
			FROM users_cursor
			INTO @RowCount
		END

		CLOSE users_cursor

		DEALLOCATE users_cursor

Please help me to do solve this ..

Thanks in advance .



Viewing all articles
Browse latest Browse all 7129

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>