Migrating a SQL Anywhere database using triggers - triggers

I was trying to create a trigger using SQL Anywhere which would export the database so it can be transferred to another database. The query is the following:
SELECT * FROM Estudante ;
OUTPUT TO 'c:\Users\XUSERX\Desktop\file.xml'
QUOTE '"'
FORMAT XML ;
When running it shows error on the line
OUTPUT TO 'c:\Users\XUSERX\Desktop\file.xml'

The OUTPUT TO syntax is only available in the dbisql utility, not in stored procedures or triggers. Look at the UNLOAD statement in the docs.
Disclosure: I work for SAP in SQL Anywhere engineering.

Related

How to guard execution when 'USE' not supported in SQL

When I'm sketching out SQL statements I have a file of all the queries I have used to analyse my live data. Each time I write a new statement or group of statements at the end of the fileI select them and click 'execute' to see the results. I'm paranoid that I may forget the selection stage and accidentally run all the queries sequentially in the entire file and so I head the file with the line
USE FakeDatabase
so that the queries will fail as they will be run against a non-existing database. But no, instead I get the error
USE statement is not supported to switch between databases
(N.B. I am using SQL Server Management Studio v17.0 RC1 against a v12 Azure SQL Server database.)
What tSQL statement can I use that will prevent further execution of tSQL statements in a file?
use is not supported in AZURE...you can try below ,but there can be many options depending on your use case
Replace use Database with below statement
if db_name() <>'Fakedatabase'
return;
You could, instead, put something like this in each script:
IF ##SERVERNAME <> 'Not-Really-My-Server'
BEGIN
raiserror('Database Name Not Set', 20, -1) with log
END
-- Rest of my query...

TSQL call query from wtihin a query in same database

I am blaming this on Monday morning, but is there some way to run a query from within a query.....
Isn't there some sort of command like
EXEC sp_ myOtherQuery.sql
I realize this lacks all sorts of error handling just wondering if there was some way to do this.
I had similar situation and used something like:
declare #cmd nvarchar(max)
declare #spname nvarchar(256)
set #spname=<your_database_name> + '.dbo.' + 'sp_executesql'
set #cmd='your query text'
execute #spname #cmd
If you're doing this in SQL Server Management Studio:
Turn on SQLCMD mode using the query menu. Then use the :r directive to include the other sql file into the one you're editing.
--sql statements
:r "C:\Full\Path\To\Sql\File.sql"
--More sql statements
This is effectively an include directive. The contents of the sql file will be inserted in your script at run time and the whole thing will be run as one.
This works only in SSMS. It isn't something you can do in a stored procedure.

What is the purpose of pgScript in PostgreSQL?

I have failed to understand the need for pgScript, which could be executed using pgAdmin tool. When it should be used? What it can do that plpgSQL cannot do? What is equivalent of it in Microsoft SQL Server?
pgScript is a client-side scripting language, while pl/PgSQL runs on the server. This means they have entirely different use cases. For example, PgScript can manage transaction status while pl/PgSQL cannot, but pl/Pgsql can be used to extend the language of SQL while pgScript cannot do that.
Additionally it means the two will handle many other things quite differently ranging from query plans to dynamic SQL, and while pgScript requires round trips between queries pl/Pgsql does not.
One use for pgScript is to define variables and use them later in your SQLs.
For example, you could do something like this:
declare #mytbl, #maxid;
set #mytbl = 'sometable';
set #maxid = 2500;
set #res = select count(*) from #mytbl where id <= #maxid;
print #res;
This approach is to just have any variables you want to change at the top of your script, rather than those getting buried deep inside complex SQL queries.
Of course, pgScript is a feature available only inside PgAdmin III client like #{Craig Ringer} mentioned in his comment.
I used DDL script generator from Toad Data Modeler.
I ran the script using normal query execution in PgAdmin-III but it kept giving me error:
"ERROR: relation "user" already exists SQL state: 42P07".
I ran Execute pgScript in PgAdmin-III and it worked fine.

ADO: Execute multiple TSQL using connection and command object

For a particular installation of my application, I need to create the database and the schema on the SQL server from the installer itself. I have a custom installer through which I have been able to detect and install the pre-requisites and the software. The user is prompted to give the IP of the database server and the username and password. Behind the scene, I create a connection and a command object. I keep the queries in different files. I use a reader and read the content of the file and set the content of the file to the CommandText of the command object. The typical content of the file is like following:
create database mydatabase
Go
Use mydatabase
Go
EXEC sp_MSforeachtable #command1 = "DROP TABLE ?"
Now the issue is the first statements get executed but it gives error after that. The error that is shown is: "syntax error near 'GO'". I tried removing the GO statement and also tried ending the sql statements with semi-colon. The error in this case is "Database 'mydatabase'does not exist. Make sure that the name is entered correctly.".
However if I keep a single statement in the file, it works fine.
Can somebody help me?
As you can see at http://technet.microsoft.com/en-us/library/aa258908%28SQL.80%29.aspx
Remarks
GO is not a Transact-SQL statement; it
is a command recognized by the osql
and isql utilities and SQL Query
Analyzer.
So this is the cause of your problems when you run it using the SqlCommand from .Net.
In my opinion you have two options:
1) Execute the instructions one by one. Maybe use a separator in your files, then split the SQL statements and execute them sequentially using a for/foreach.
2) Use Server class from SQL Server Management Objects (SMO) that should allow you to execute the script containing "Go" statements.
You can execute more than one sql command statement by simply adding a ";" at the end of each command instead of a "GO" statement.
Example:
cmd.CommandText = #" Update TableA Set ColumnA = 'Test' Where ID = 1;
Update TableB Set ColumnA = 'Second line' Where ID = 2;
";

DB2 Equivalent to SQL's GO?

I have written a DB2 query to do the following:
Create a temp table
Select from a monster query / insert into the temp table
Select from the temp table / delete from old table
Select from the temp table / insert into a different table
In MSSQL, I am allowed to run the commands one after another as one long query. Failing that, I can delimit them with 'GO' commands. When I attempt this in DB2, I get the error:
DB2CLI.DLL: ERROR [42601] [IBM][CLI Driver][DB2] SQL0199N The use of the reserved
word "GO" following "" is not valid. Expected tokens may include: "".
SQLSTATE=42601
What can I use to delimit these instructions without the temp table going out of scope?
GO is something that is used in MSSQL Studio, I have my own app for running upates into live and use "GO" to break the statements apart.
Does DB2 support the semi-colon (;)? This is a standard delimiter in many SQL implementations.
have you tried using just a semi-colon instead of "GO"?
This link suggests that the semi-colon should work for DB2 - http://www.scribd.com/doc/16640/IBM-DB2
I would try wrapping what you are looking to do in BEGIN and END to set the scope.
GO is not a SQL command, it's not even a TSQL command. It is an instruction for the parser. I don't know DB2, but I would imagine that GO is not neccessary.
From Devx.com Tips
Although GO is not a T-SQL statement, it is often used in T-SQL code and unless you know what it is it can be a mystery. So what is its purpose? Well, it causes all statements from the beginning of the script or the last GO statement (whichever is closer) to be compiled into one execution plan and sent to the server independent of any other batches.