Sybase: How to string multiple T-SQL commands together - tsql

I want to execute the following commands in a single DataAdapter command:
SET ROWCOUNT 10; SELECT * FROM dbo.SYB_CO_COLOUR ;SET ROWCOUNT 0
but this isn't accepted by my Sybase DB, not even in the Interactive SQL tools that ships with the DB.
I have not changed the command_delimiter from the semi-colon default so I am surprised that I get the error
Incorrect Syntax near ';'
What am I missing?
Thanks

Remove semicolon and seperate them by space like:
SET ROWCOUNT 10 SELECT * FROM dbo.SYB_CO_COLOUR SET ROWCOUNT 0
It outputs 10 rows and resets rowcount back.

Related

How to assign value to a variable in select query in postgresql

I am migrating from mssql to postgresql
I am trying to assign a value to a temporary variable in select query
Below is the query in mssql
select flag = 0
It gives 0 as output wit flag as column identifier in mssql
I tried the following in postgresql
select flag := 0
select flag [:=0]
It says syntax error at or near :=
Does anybody know where I am going wrong?
It gives 0 as output wit flag as column identifier in mssql
Postgres honors the SQL standard and a column alias is defined with the AS keyword there.
So to get a column named flag with the value zero use the following:
select 0 as flag
flag = 0 in standard SQL (and Postgres) is a boolean expression that compares the the column flag with the value zero and returns either true, false or null depending on the column's value.

TSQL (SSMS) conditionally display result of query only conditionally

Is it possible to display in the results-window of a query in TSQL (SSMS) window conditionally?
For example, display column-header and result of:
SELECT COUNT(1) AS ourCount FROM [ourDatabase].[dbo].[ourTable]
only if it is > 0
NOTE: We use SQL Server 2008, r-2
This is in the context of a larger system of queries with many results. I don't want to clutter the results if this particular query has a zero-value. Of course, the concept could be generalized for other situations.
So I am monitoring the query output, and one could think of the results as an 'alert' to myself (informally).
This will push the result into a variable, and then only display it if it's greater than zero, you could also use PRINT, etc.
DECLARE #Count INT;
SELECT #Count = COUNT(1) AS ourCount FROM [ourDatabase].[dbo].[ourTable];
IF #Count > 0
BEGIN
SELECT #Count;
END;
If the answer is <= 0 then you will see nothing but the row count in the message part of SSMS. You can even stop this by adding:
SET NOCOUNT ON;
...at the top of your script, but remember to add:
SET NOCOUNT OFF;

postgres how to refer to table names with minus signs

I was wondering if there is a syntax to refer to a table name with minus signs in it. For example, I imported a table called v-water-a using shp2psql, and then did:
select * from v-water-a limit 1;
and I got an error:
ERROR: syntax error at or near "-"
LINE 1: select * from v-water-a limit 1;
The same query works if the table was named v_water_a. Also, the table v-water-a is visible in pgadmin3. I tried quote the name with single quotes, but it didn't work.
Should I quote the table name somehow? or is it the name illegal?
This is with PostgreSQL 9.3.5 under Ubuntu 14.04.
Use double-quotes:
select * from "v-water-a" limit 1;
Documentation: http://www.postgresql.org/docs/current/static/sql-syntax-lexical.html

sybase cursors in a trigger

I am trying to use a cursor in a trigger on a sybase ASE 15.0.3 system running on Solaris. The purpose for this is that I want to know which column of a table is getting updated. This information I then save in an admin table for further lookups.
create trigger test_trigger on my_table for update as
set nocount on
/* declare cursor */
declare #colname varchar(64)
declare column_name cursor for
select syscolumns.name from syscolumns join sysobjects on (sysobjects.id = syscolumns.id) where sysobjects.name = 'my_table'
/* open the cursor */
open column_name
/* fetch the first row */
fetch column_name into #colname
/* now loop, processing all the rows
** ##sqlstatus = 0 means successful fetch
** ##sqlstatus = 1 means error on previous fetch
** ##sqlstatus = 2 means end of result set reached
*/
while (##sqlstatus != 2)
begin
/* check for errors */
if (##sqlstatus = 1)
begin
print "Error in column_names cursor"
return
end
/* now do the insert if colum was updaed */
if update(#colname)
begin
insert into my_save_table (login,tablename,field,action,pstamp)
select suser_name(),'my_table',#colname,'U',getdate() from inserted
end
/* fetch the next row */
fetch column_name into #colname
end
/* close the cursor and return */
close column_name
go
Unfortunately when trying to run this in isql I get the following error:
Msg 102, Level 15, State 1:
Server 'my_sybase_server', Procedure 'test_trigger', Line 34:
Incorrect syntax near '#colname'.
I did some investigations and found out that line 34 means the following statement:
if update(#colname)
then I tried to just check on 1 column and replaced it by
if update(some_column_name)
That actually worked fine and I don't have any other idea how to fix that. It looks like the update() function somehow not allows to contain a variable. I did not find any additional information on the sybase books or anywhere else in google ect. Does anybody may find a solution for this? Is it may a bug? Are there workarounds for the cursor?
Thanks for any advice
The problem is that update(#colname) is something like update('colname') and needs to be update(colname). In order to you achieve that, you need to use Dynamic SQL.
I've already saw the documentation and it's possible to use:
Dynamically executing Transact-SQL
When used with the string or char_variable options, execute concatenates the supplied strings and variables to execute the
resulting Transact-SQL command. This form of the execute command may
be used in SQL batches, procedures, and triggers.
Check this article for an example on how to use dynamic sql!
If it is not a problem to recreate the trigger every time the table is altered (columns added/dropped) you may just generate the body for your trigger with such query
select
'if update('+c.name+')
set #colname = '''+c.name+'''
'
from syscolumns c where id = object_id('my_table')

Sybase IQ Pagination

I wonder if anyone has a solution to the following requirement. I have a stored procedure which returns a result set of for example 1000 rows. Now I need to limit this to 100 rows at a time. So I will pass in a start and end index value and I only want the records between the start index rowcount and the end index rowcount
So for example my stored procedure call signature looks like this:-
stp_mystoredproc(startIndex INTEGER, endIndex INTEGER)
So if I set startIndex = 100 and endIndex = 200 then I want the stored procedure to return the records in rows 100 to 200 out of the total reset set of 1000.
My first attempt is put the result set in a temp table with an identity column then select based on the identity the range I need but this is somewhat slow. I know Oracle supports pagination so you can page through your result set. Anyone know if Sybase IQ (v12.6 or v12.7) supports something similar?
The end goal is to page through the entire result set (1000 records) but in 100 row pages at a time.
I don't know sybase. But maybe you could do something like this
myproc(#count int, #lastid int)
select top #count *
from MyTabel
where id > #lastid
order by id
first call
exec myproc(100, 0)
gives you something like
3 appels
4 banana
..
..
..
346 potatto
next call
exec myproc myproc(100,346)
Sybase IQ and Sybase SQL Anywhere share the same query execution engine and (mostly) SQL syntax, so you can generally use SQL Anywhere syntax. Try this:
select top (endIndex-startIndex) start at startIndex from <query>
I'm not sure if you can use an expression in the top clause, so you may have to create a string and use execute immediate.
See http://dcx.sybase.com/index.html#1201/en/dbreference/select-statement.html