From powershell we are running the following command to create a Database. The database name starts with a number:
SQLCMD -S Z0EUW1WLSQL031\PRUEBA -U rdb00001 -P "1234" -v datos="`"D:\Datos\`"" dbName="3DataBase" log="`"E:\Logs\`"" collation="Latin1_General_CI_AS" -i 10_CreateDB.sql -o 10_CreateDB.log
The script "10_CreateDB.sql" contains this:
CREATE DATABASE $(dbName) ON PRIMARY
( NAME = N'$(dbName)', FILENAME = N'$(datos)$(dbName).mdf' )
LOG ON
( NAME = N'$(dbName)_log', FILENAME = N'$(log)$(dbName)_log.ldf' )
COLLATE $(collation)
GO
We are getting this error:
Msg 102, Level 15, State 1, Server Z0EUW1WLSQL031\PRUEBA, Line 1
Incorrect syntax near '3'.
$ArgumentList = #(
'-S Z0EUW1WLSQL031\PRUEBA',
'-U rdb00001',
'-P "1234"',
'-v datos="D:\Datos\"',
'dbName="3DataBase" ',
'log="E:\Logs\"',
'collation="Latin1_General_CI_AS"',
'-i 10_CreateDB.sql',
'-o 10_CreateDB.log'
)
Start-Process "fullPathHere\SQLCMD.exe" -ArgumentList $ArgumentList
Related
I have this query, and in the table/column ttransactionlog_1/occurdatetime, it returns the time value with a .000 at the end, can someone tell me how to remove that from each row? here is a line from the outputfile showing the .000 at the end of the time
0,112213,2021-03-11 14:00:00.000,Santiago,Melody,AdminClock.
Here is the query:
sqlcmd -S clock\punch -d test -U xxx -P xxxxx -Q "Select Distinct TTransactionLog_1.DecisionTimeInterval, TTransactionLog_1.UserID, TTransactionLog_1.OccurDateTime, TTransactionLog_1.lastname, TTransactionLog_1.firstname, TSystemLog1.Name
From TTransactionLog_1 Inner join TSystemLog1 On TTransactionLog_1.NodeID=TSystemLog1.NodeID where TSystemLog1.NodeID = 3 and TTransactionLog_1.OccurDateTime > = dateadd(hh, -1, getdate())
" -s "," -h-1 -W -o "C:\ATR\adminreport.csv"
Thanks in advance!
Does just using Convert on the OccurDateTime value work for you?
Example
declare #OccurDateTime datetime='20210311 14:00:00.000'
select Convert(varchar(19),#OccurDateTime,121)
PostgreSQL version : 11.1
Platform : OSX Mojave 10.14.1
That's my SQL code:
COPY (select nom,prenom,num_carte,pdv_carte,email,date_naissance from compte where num_carte != '' order by id_compte) TO :export_file WITH DELIMITER AS ';' CSV FORCE QUOTE * ENCODING 'UTF-8';
That line is in a .sql file called by shell script like this :
psql --dbname=test -U postgres --set adresses=$DATA_ADRESSE --set export_file="$EXPORT_FILE" --file=$ANO_SQL 1>>$ANO_LOG
With EXPORT_FILE variable declared like that :
export EXPORT_FILE="'export_for_fid.csv'"
Tried many solutions but none worked, always the same syntax error:
ERROR: syntax error at or near ""
LINE 1: ...where num_carte != '' order by id_compte) TO 'export_for_fid.csv' WITH D...
^
Instead of using --file and --set arguments, you could use a here-document in your shell script: (NOTE: I replaced the COPY by a \COPY )
#!/bin/sh
EXPORT_FILE="export_for_fid.csv"
DB_NAME="test"
psql --dbname=${DB_NAME} -U postgres <<OMG
\COPY (select nom,prenom,num_carte,pdv_carte,email,date_naissance from compte where num_carte <> '' order by id_compte) TO '${EXPORT_FILE}' WITH DELIMITER AS ';' CSV FORCE QUOTE * ENCODING 'UTF-8';
OMG
#eof
Trying to assign output from SQL query on an object containing special characters to a variable in shell script.
Running directly on the database:
db2 -x 'select count(*) from <SCHEMA>."/BIC/TEST"'
11000
Yet when I include this in script I need to use double quotes as I am using variables passed into the sql. Using single quotes
Output=$(db2 -x 'select count(*) from ${_SCHEMA}."/BIC/TEST"')
echo -e "$Output"
Results in:
SQL20521N Error occurred processing a conditional compilation directive near
"_". Reason code="7". SQLSTATE=428HV
When I use double quotes I hit:
SQL0104N An unexpected token "'/BIC/TEST'" was found following "ount(*)
Tried to escape the double quotes using another set of double quotes:
db2 -x 'select count(*) from ${_SCHEMA}.""/BIC/TEST""'
But this doesn't seem to work in script. It works for tables where there is no special characters/requirement to encase in quotations.
Any help is appreciated.
The code below works fine for me, notice the escaped quotes. If it fails for you, you need to give more details of your DB2-version and the DB2-server operating system platform.
#!/bin/ksh
db2 connect to sample
(($? > 0 )) && print "Failed to connect to database" && exit 1
db2 -o- "drop table \"/bin/test\" "
db2 -v "create table \"/bin/test\"(a integer)"
(($? > 0 )) && print "Create table failed" && exit 1
db2 -v "insert into \"/bin/test\"(a) values(1),(2),(3),(4)"
(($? > 0 )) && print "insert rows failed" && exit 1
db2 -v describe table \"/bin/test\"
typeset -i count_rows=$(db2 -x "select count(*) from \"/bin/test\"" )
(($? > 0 )) && print "query count rows failed" && exit 1
print "\nRow count is: ${count_rows}\n"
db2 -o- connect reset
suppose below is my query.
DECLARE #INT INT
SET #INT = (SELECT COUNT(1) FROM MyTable)
IF (#INT = 0)
RAISERROR('Fail',16,1)
I want to use this query and create a SQL Server job using "Operating System (CmdExec)" type.
I want to SQL Job step to be successful if #INT <> 0 and fail if #INT = 0.
How can I write the cmd?
My try (and it doesn't work)...the server is different than where I will be setting up the job.
sqlcmd -S 123.45.67.890 -E -V15 -d MyDB -Q "DECLARE #INT INT
SET #INT = (SELECT COUNT(1) FROM MyTable)
IF (#INT = 0)
RAISERROR('Fail',16,1)" -b
Job failure msg.
Executed as user: LocalSVR\USER. Msg 105, Level 15, State 1, Server RemoteSVR, Line 1 Unclosed quotation mark after the character string 'DECLARE #INT INT '. Process Exit Code 15. The step failed.
Please note, the step fails for the same reason when MyTable has 1+ record(s).
Update: As per Scott's suggestion, I changed the formatting (removed line breaks) and it seems to work. #Scott, not sure how to mark your comment as answer. If you can write a response, I will mark it as answer. Cheers!!!
You're trying to execute multiple lines as opposed to a single query. You can get round that by using IF NOT EXISTS
SQLCMD -b -S"MyServer" -d"MyDatabase" -Q"IF NOT EXISTS(SELECT * FROM MyTable) RAISERROR('Fail',16,1)"
Note that the -b (On error batch abort) option means that SQLCMD itself will fail giving you a Trappable ErrorLevel
i am facing error wile running this in sql server 2000
bcp "select dmedocno ,dmename,dmeauthor,dmekeywords from document
where repositoryid=4 and dmedocno between 1 and 60000 order by
dmedocno" queryout c:\LR_Query29_cutover_19_August.csv -Swsapp0772 -T -c -t","
Error is:
Server: Msg 103, Level 15, State 7, Line 1
The identifier that starts with 'select dmedocno ,dmename,dmeauthor,dmekeywords from document where repositoryid=4 and dmedocno between 1 and 60000 order by dmed' is too long. Maximum length is 128.
Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near 'queryout'.
why your sql server treats the query as identifier? hm...
Check default QUOTED_IDENTIFIER option for the server, try to set it to OFF