I'm trying to make a script that sends mail to someone (with Unix) to receive the output of a statement. What I selected in that statement to send to the mail.
I already have a connection made through Teradata, I kept trying to make this reference but it doesn't work ..
If I'm trying to do with spool, like this and then to sent the email from that text file (probably teradata doesn't support spool):
spool file_name.txt
select * from db;
spool off;
This gives me an error:
Failure 3706 Syntax error: expected something between the beginning
of the request and the 'spool' keyword.
My question is how can I do this?
Related
Error is not evident - not sure how to debug the except section
import pyodbc
driver='{ODBC Driver 17 for SQL Server}'
def exec_sqlcmd(sqlcmd,cnxn):
try:
cursor=cnxn.cursor()
cursor.exec(sqlcmd)
cnxn.commit()
cursor.close()
except:
print("debug in sql: " + sqlcmd)
server='tcp:server.domain.ou.ou'
database='mydb'
cnxn=pyodbc.connect(
Trusted_Connection='Yes',
Driver='{ODBC Driver 17 for SQL Server}',
Server='10.10.10.01,1433',
Database='mydb'
)
#cursor=cnxn.cursor()
profilename='serverdbmailprofilename'
recipients='me#mydomain.com'
subject='test me'
body='did you get this'
query="""Exec msdb.dbo.sp_send_dbmail
#profile_name=profilename,
#recipients=recipients,
#subject=subject,
#body=body"""
exec_sqlcmd(query,cnxn)
cnxn.close()
Error is not evident - not sure how to debug the except section - works fine in ssms and sends the email. Not a python superstar - looking for a way to debug the error in except section.
If you decide to paste and test - be sure to put the correct server name, domain name, email and so on to get a similar result.
Does the query require the three quotes? Error is different using just one single quote.
Doesn't appear the cursor needs to be pre-declared to work. There is an error from a script running similar queries (not this one) pointing to WinError 10060 indicating a peering issue in AWS. The peering appears to be correct and the security groups are open correctly. SQL server (through 1433 and subsequently pyodbc) should be working on the same port. SQL works but not pyodbc.
Is there a different port used by pyodbc that isn't port 1433?
I struggled with this error condition. In many examples, I see the short hand cursor.exec(sqlcmd) One modification made this example work.
cursor.execute(sqlcmd)
Having said that, I am still looking for the python way to output the SQL error.
Im trying to add another data in a from table in a separate database to my script,
but I keep getting this error all time.
My script
connect database "chris.db" .
run chrisf.p
disconnect databse.
The error I'm getting
How can I get round this issue?
Thank you.
The word "database" is not part of the syntax for the CONNECT statement.
CONNECT "chris".
is the correct syntax.
The OpenEdge documentation for CONNECT is here: https://documentation.progress.com/output/OpenEdge117/openedge117/?_ga=2.93982683.75218856.1547464117-1040589272.1546786181#page/dvpin%2Fthe-connect-statement.html
I'm not sure what you are trying to do with:
run chrisf.p disconnect databse.
but that will run an external procedure called "chrisf.p" and pass 2 "compile on the fly" parameters with values of "disconnect" and "databse". (I'm pretty sure that's not really what you intend.)
We are retrieving the output from table through DB link by executing a stored procedure and input parameters which worked previously and got the output in asp.net application.But now we noted that outputs through DB links are getting trimmed say if status is 'TRUE' ,we are getting as 'TRU' etc why the output values are getting trimmed.The only change we did recently was we changed one of the type of input parameter from number to varchar at the receiving remote side,But i don't think that is the issue??whe we execute the stored procedure remotely on the table.It is giving proper output but through DB link outputs are getting trimmed.ANy one has any idea about this issue??
My oracle client is having issues,i reinstalled and it worked fine.Only my system was having issues.so i desided to reinstall
I have a long script with sql statements to run on teradata. I want the script to keep running until the end and save the errors in a log file and that it will stop on every error. How can I do it?
thanks
Assuming you are using Teradata SQL Assistant:
Click on Tools in the menu bar, then Options, then Query. There is a checkbox that says "Stop query execution if an SQL error occurs"
To get the most recent error hit F11. Otherwise, from the menu bar click Tools, then show history. Double click on the row number on the left side of one of the history records and it will bring up a screen with the result messages for each statement. You can also query this sort of info directly from one of the QryLog views in DBC.
Errors can be of multiple types, some can be by-passed and some cannot be. For example, with native Teradata Tools and Utilities you can make a script ignore run-time errors, or even syntax errors, but generally it is impossible to ignore network connectivity errors and still get remaining part of your queries executed.
Generally in such scenarios, you want to use the BTEQ tool for executing the SQL in which you can ignore the execution errors. BTEQ is a standard Teradata tool which can be downloaded from Teradata website for free and is commonly installed by users querying Teradata through plain SQL.
To create a workable BTEQ script simply copy paste all of your queries into a plain text file, separate all queries with semicolons ; and on the very top of that plain text file add a logon statement as stated below
.logon Teradata_IP_Address/your_UserName,your_Password;
example script:
.logon 127.0.0.1/dbc,dbc;
/*Some sample queries. Replace these with your actual queries*/
SELECT Current_Timestamp;
CREATE TABLE My_Table (Dummy INTEGER) PRIMARY INDEX (Dummy);
So BTEQ got you through the execution errors. To avoid network connectivity issues, ideally you want to execute that on a server which has a constant connection to Teradata and with Teradata Tools and Utilities installed. Such a server may be called as ETL server, landing server, edge node or managed server (or something else, depending on your environment). You will definitely need login credentials to that server (if you don't already have access). Preferable commands to execute a bteq script are
Windows: bteq < yourscriptname >routine_logfile 2>error_logfile
Linux (bash/ksh): nohup bteq < yourscriptname >routine_logfile 2>error_logfile &
Make sure not to close the command prompt if you are on windows. On Linux you can close the current window or even terminate your network session with your ETL server if you use the recommended command.
If you see a warning about EOL line found at the end of your logs, just ignore it; it is because for simplicity I ignored some optional BTEQ statements which ensure cleaner exit.
I need help logging errors from T-SQL in SQL Server 2000. We need to log errors that we trap, but are having trouble getting the same information we would have had sitting in front of SQL Server Management Studio.
I can get a message without any argument substitution like this:
SELECT MSG.description from master.dbo.sysmessages MSG
INNER JOIN sys.syslanguages LANG ON MSG.msglangID=LANG.msglangid
WHERE MSG.error=#err AND LANG.langid=##LANGID
But I have not found any way of finding out the error arguments. I want to see:
Constraint violation MYCONSTRAINT2 on table MYTABLE7
not
Constraint violation %s on table %s
Googling has only turned up exotic schemes using DBCC OUTPUTBUFFER that require admin access and aren't appropriate for production code. How do I get an error message with argument replacement?
In .Net, retrieving error messages (and anything output from print or raiserror) from sql server is as simple as setting one property on your SqlConnection ( .FireInfoMessageEventOnUserErrors = True) and handling the connection's InfoMessage event. The data received by .Net matches what you get in the Messages window in the SQL Server Management Studio results grid.
All the code goes in the function that handles the event, and you can abstract that so that all your connections point to the same method, so there's nothing else to change in the rest of the app aside from the two lines of code when you create new connections to set the property and event (and you have that abstracted away so you only need to do it in one place, right?)
Here is a link to what I consider the definitive error guide for SQL Server.
http://www.sommarskog.se/error-handling-I.html
In certain circumstances SQL Server will continue processing even after an error. See the heading labeled What Happens when an Error Occurs? from the previous link.
Look in Books on-line for Raiserror (Described)
You will find the syntax looks like this:
RAISERROR ( { msg_id | msg_str } { , severity , state }
[ , argument [ ,...n ] ] )
[ WITH option [ ,...n ] ]
and the error arguments are as follows:
d or I Signed integer
o Unsigned octal
p Pointer
s String
u Unsigned integer
x or X Unsigned hexadecimal
Any language from VB onwards has the ability to catch these and let you to take the appropriate action.
Dave J
Any chance you'll be upgrading to SQL2005 soon? If so, you could probably leverage their TRY/CATCH model to more easily accomplish what you're trying to do.
The variables exposed in the catch can give you the object throwing the error, the line number, error message, severity, etc. From there, you can log it, send an email, etc.
FORMATMESSAGE (it also exists in SQL Server 2000) allows you to build up messages into their final format from the sysmessages templates like above.
However, the RAISERROR command (which is pretty much what the database engine itself uses internally calls when you have an error) already sends the completed text which can be trapped and logged in the client. SSMS is a client and does not generate it's own messages: all message come from the database engine.
However, I gather you want to log the T-SQL error using T-SQL. Frankly, you can't on SQL Server 2000. Too many errors are batch and scope aborting to reliably log anything.
You have to be on SQL Server 2005 to use TRY/CATCH/ERROR_MESSAGE, or you trap in the client and then using something like log4net to log back to SQL Server.