How to increment as 001,002 etc In SQL Server
-1
votes
2
answers
150
views
i have created an sp to export scripts i stored in a table to text files. What this sp do is if I give 100 as count then each file will have 100 scripts from the table. i am saving the file name as objects_1 ,objects_2 and so on. But what I need is . If I have 100 files. Then the 1st file should be names as objects_001 ,objects_002 , and the last file should be objects_100 . Can someone suggest an idea to do that . ,And please confirm this sp logic is correct .
Thanks.
alter PROCEDURE USP_GenerateFile_Packets_new(@count int)
AS
BEGIN
DECLARE @ExportQry NVARCHAR(500)
DECLARE @sql NVARCHAR(500)
DECLARE @ExportPath NVARCHAR(100)
SET @ExportPath='C:\Upgrade\Scripts\'
DECLARE @BCPFileName NVARCHAR(50)
DECLARE @ReturnValue NVARCHAR(50)
DECLARE @K INT=1
declare @minval int=(select min(id) from vsl.ScriptTable)
--DECLARE @I INT=(SELECT COUNT(*) from [UpgradeDB].vsl.ScriptTable)
declare @count1 int=(select @count-1)
WHILE @minval ='+convert(nvarchar(100),@minval)+' and id<='+convert(nvarchar(100),@minval)+'+'+convert(nvarchar(100),@count1)+' order by id'
SET @sql = 'bcp "' + @ExportQry + ' " queryout ' + @ExportPath + 'OBJECTS_'+convert(nvarchar(100),@k)+'.txt -c -t^| -T -S ' + @@SERVERNAME;
--select @sql
EXEC @ReturnValue = master..xp_cmdshell @sql
SET @minval=@minval+@COUNT
set @k=@k+1
END
END
--USP_GenerateFile_Packets_new 1500
Asked by Subin Benny
(77 rep)
Jan 17, 2023, 07:17 AM
Last activity: Jan 17, 2023, 02:08 PM
Last activity: Jan 17, 2023, 02:08 PM