Michael
Michael

Reputation: 869

Insert Query Works In SSMS but not SQLCMD

I have a SQLCMD set to run in a batch script:

sqlcmd -S <SQL Server Instance> -U <USER NAME> -P <PASSWORD> -i "<Path to .sql file>"

Note I need the quotes in the file path because it has spaces

The .sql file is something like this.

INSERT  INTO Table2 (Col1, Col2, Col3)
SELECT Col1 
,Col2
,Col3
FROM Table1

When I execute the SQL in Sql Server Management Studio it runs fine. I put a pause in the .bat file and it gives the message.

Msg 2714, Level 16, State 6, Server , Line 1 There is already an object named 'Table2' in the database.

Here is the query (obfuscated of course):

INSERT  INTO Table1 (Col1, Col2,Col3,Col4,Col5,Col6)
SELECT Col1
,Col2
,Col3
,Col4
,Col5
,cast(getdate() as date) as 'Col6'
FROM OrigTable 
WHERE exists(SELECT *
FROM ChkTable
WHERE DateCol > '20140101'
and OrigTable.Col1 = ChkTable.Col1)
and not exists(SELECT *
FROM Table1 inner join (SELECT max(Col6) as 'TopDate'
                          ,Col1
                          FROM Table1
                          GROUP BY Col1) a
on Table1.Col6 = a.TopDate
and Table1.Col1 = a.Col1
WHERE Table1.Col1 = OrigTableCol1
and (Table1.Col5 = OrigTable.Col3
or isnull(Table1.Col4,1) = isnull(OrigTable.Col4,1)))

Upvotes: 0

Views: 705

Answers (1)

Eliseu Marcos
Eliseu Marcos

Reputation: 311

Remove the table creation script from the directory you are using.

Upvotes: 1

Related Questions