Here's what I'm trying to do: I have a lookup table im SQL with a field that contains filename. I need to iterate through each of the records in this table and find the associated records from another table (using the filename column as the key field) and then save each set of matching records as a CSV file locally on my computer. I tried the below cursor and it's not working. Can someone assist?
DECLARE @name VARCHAR(50) -- database name
DECLARE @path VARCHAR(256) -- path to save file
DECLARE @fileName VARCHAR(256) -- filename for output file DECLARE @fileDate VARCHAR(20) -- used for file name
SET @path = 'C:\Output\'
SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112)
DECLARE db_cursor CURSOR FOR
SELECT * FROM dbo.filenamelookup fl JOIN dbo.recordstable rt ON fl.filename = rt.filename OPEN db_cursor
FETCH NEXT FROM db_cursor INTO #temp
WHILE @@FETCH_STATUS = 0
BEGIN
SET @fileName = @path + @name + '_' + @fileDate + '.csv'
BACKUP DATABASE #temp TO DISK = @fileName
FETCH NEXT FROM db_cursor INTO @name
END
CLOSE db_cursor
DEALLOCATE db_cursor
Aucun commentaire:
Enregistrer un commentaire