Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
jallah_88
Starting Member
12 Posts |
Posted - 2011-02-09 : 05:38:41
|
| sqlcmd -S Servername -d dbname -E -i driveletter:\file.file\sql.commands\testing.sql -o driveletter:\file.file\results.csv -w 999 -s","Hi guysI am trying to export an sql file to csv file using SQLCMD. I have come up with this so far, but keep getting errormsg102, Level 15, State 1, Server (servername), Line 1Incorrect syntax near '-'Please help me :)Cheers,Martin |
|
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2011-02-09 : 06:14:43
|
| I think you will need to post the content of the testing.sql file. The error seems to be coming from the database engine...- LumbagoMy blog-> www.thefirstsql.com |
 |
|
|
jallah_88
Starting Member
12 Posts |
Posted - 2011-02-09 : 08:24:23
|
| Okay.. thanks for the quick reply :) Oh and my intention is to find missing numbers in a column and i found this the easiest way.csv (collected data) -> sql (do the necessary queries) -> csv (results)use Monitor-- Creating permanent table to put csv file data intoIF EXISTS(SELECT 1 FROM sys.objects WHERE OBJECT_ID = OBJECT_ID(N'importcsv') AND type = (N'U')) DROP TABLE importcsv CREATE TABLE importcsv ( action varchar(10), src varchar(80), dst varchar(80), service varchar(80), fwrule int, xlatesrc varchar(40) )-- Insert csv data to importcsv table BULK INSERT importcsv FROM 'Driveletter:\file.file\test.csv' WITH ( DATAFILETYPE = 'char', FIRSTROW = 2, FIELDTERMINATOR = ',', ROWTERMINATOR = '\n', MAXERRORS = 1, ERRORFILE = 'Driveletter:\file.file\Error.txt', KEEPNULLS )-- Table containing numbers from 1-700 used to find missing fw rulesDECLARE @AllRulesTable TABLE (n INT NOT NULL PRIMARY KEY) ;SET NOCOUNT ONDECLARE @Index INT ;SET @Index = 0 ; WHILE @Index <= 700BEGIN INSERT INTO @AllRulesTable(n) VALUES(@Index) ; SET @Index = @Index + 1 ;END-- Table containing present fw rules from csv file, where only one of each--fw rule is presentDECLARE @PresentRules table (id int) INSERT INTO @PresentRules (id) SELECT fwrule FROM importcsv WHERE fwrule>0 GROUP BY fwrule having count(*) > 1 ORDER BY fwruleif object_id('testing') IS NOT NULL drop table testingSELECT A.*INTO testingFROM ( SELECT A.n AS I FROM @AllRulesTable A EXCEPT SELECT A.id AS I FROM @PresentRules A) AS A |
 |
|
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2011-02-09 : 09:08:06
|
| Have you tried to run the code in the .sql-file in a query windows in SSMS? Your sqlcmd-syntax is fine, I just tried it and you should probably add the -W switch as well to trim trailing spaces but the syntax works. Try to replace the contents of the testing.sql file with "select top 10 * from INFORMATION_SCHEMA.TABLES" instead just to make sure that you can at least get something out to the csv.- LumbagoMy blog-> www.thefirstsql.com |
 |
|
|
|
|
|
|
|