Click here to Skip to main content
15,899,025 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
USE RAUSDXT02

--drop table ##tbl_STORE_LIST

select store_code INTO ##tbl_STORE_LIST from store

where

store_code not in ('722','803','8000') and

date_closed is NULL and

global_tax_code is not NULL and

store_type='S'

--select * from ##tbl_STORE_LIST

DECLARE @store_code nvarchar(20)


DECLARE store_code_cursor CURSOR FOR SELECT DISTINCT store_code FROM ##tbl_STORE_LIST
OPEN store_code_cursor
FETCH NEXT FROM store_code_cursor INTO @store_code


WHILE @@FETCH_STATUS = 0
BEGIN
EXEC XP_CmdShell 'BCP "Select * from ##tbl_STORE_LIST" queryout "\\dalsqlposdev2\RADATA\Debopam\TRAFFIC\ACTIVESTORES.txt" -c -T'

FETCH NEXT FROM store_code_cursor INTO @store_code
END
CLOSE store_code_cursor
DEALLOCATE store_code_cursor

DROP TABLE ##tbl_STORE_LIST


I want to put this above script in an SP so that I can simply use EXEC SP_name to execute the query
Posted
Comments
Maciej Los 20-Feb-14 17:37pm    
What have you tried?
Vedat Ozan Oner 20-Feb-14 19:39pm    
see https://www.google.com.tr/#q=sql%20server%20stored%20procedure%20example

1 solution

SQL
create procedure SP_ProcedureName
--(@storecode varchar(50)) //if you want to pass this as parameter, you need to pass it as comma seperated value string format
  --and you need to convert the   CSV to a table format
as
begin
USE RAUSDXT02
 
--drop table ##tbl_STORE_LIST
 
select store_code INTO ##tbl_STORE_LIST from store
 
where 
 
 store_code not in ('722','803','8000') and 
--store_code not in (@storecode) and 

date_closed is NULL and 

global_tax_code is not NULL and

store_type='S'

--select * from ##tbl_STORE_LIST
 
DECLARE @store_code nvarchar(20)
 

DECLARE store_code_cursor CURSOR FOR SELECT DISTINCT store_code FROM ##tbl_STORE_LIST
OPEN store_code_cursor
FETCH NEXT FROM store_code_cursor INTO @store_code
 

WHILE @@FETCH_STATUS = 0
BEGIN
EXEC XP_CmdShell 'BCP "Select * from ##tbl_STORE_LIST" queryout "\\dalsqlposdev2\RADATA\Debopam\TRAFFIC\ACTIVESTORES.txt" -c -T'
 
FETCH NEXT FROM store_code_cursor INTO @store_code
END
CLOSE store_code_cursor
DEALLOCATE store_code_cursor
 
DROP TABLE ##tbl_STORE_LIST
end
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900