-- assumed table is named "My_Table" with 2 columns "date" and "name" living in the database "My_Database"
DECLARE @date datetime
DECLARE @name varchar(20)
DECLARE @lastdate datetime
DECLARE @SQL varchar(1000)
SET @LASTDATE = '20991231'
DECLARE c CURSOR READ_ONLY FOR SELECT date, name from My_Table order by date
OPEN c
FETCH NEXT FROM c INTO @date, @name
WHILE @@FETCH_STATUS = 0
BEGIN
IF @lastdate <> @date
BEGIN
SET @sql = 'bcp "select * from My_Database..My_Table where date = '''+convert(varchar(8),@date,112)+'''" queryout "c:\ee\textfilename'+convert(varchar(8),@date,112)+'.txt" -T -c -CACP'
PRINT @sql
EXEC master..xp_cmdshell @sql
SET @lastdate = @date
END
FETCH NEXT FROM c INTO @date, @name
END
CLOSE C
DEALLOCATE C
|