I'm a SQL Newbie and I'm trying to figure out how to use a Parameter with SQL Thru Command-Line
For my boss/staff I have written Batch Files to run SQL Code for them to export data and the like. In Access all I have to do is [Paramerter] and it prompts for data to be entered.
The @State Variable I'd like to be able to be set dynamically. I'd like the batch file to ask for State and Query use that information. I have no idea how to do it.
Batch File
sqlcmd -E -S ServerName -i C:\Lead$\SQL\MakeSTPhoeLists.sql
pause
The SQL File
Use LeadsDb
Go
Declare @State VarChar(2)
Set @State = 'DE'
DELETE FROM tblzExportPhone
INSERT INTO tblzExportPhone ( Phone )
SELECT tblLeads.Phone
FROM tblLeads
WHERE tblLeads.ST = @State
Declare @FileName VarChar(100)
Set @FileName = 'H:\Leads\Storage\STLists\' + @State +'StatePhoneList.csv'
DECLARE @bcp_cmd4 VARCHAR(400) = ' BCP.EXE LeadsDb..tblzExportPhone out '
SET @bcp_cmd4 = @bcp_cmd4 + 'H:\Leads\SQL\Formats\PhoneTmp.csv' + ' -T -f H:\Leads\SQL\Formats\tblzExportPhone.fmt'
SET @bcp_cmd4 = @bcp_cmd4 + ' & Copy /b H:\Leads\SQL\Formats\ExPhone.csv+H:\Leads\SQL\Formats\PhoneTmp.csv ' + @FileName + ' /y'
Set @bcp_cmd4 = @bcp_cmd4 + ' & Del H:\Leads\SQL\Formats\PhoneTmp.csv'
Thank You.