Zero length single line comment leads to parse error - oracle-sqldeveloper

not of utmost importance, but I would be interested if someone can explain this behaviour that I get for zero length single line comments both on 11g and 12c, Oracle SQL Developer 17.3.1.279. I especially find the different results from Test 3 (--a) and Test 5 (---) interesting:
-- Test 1
select * from dual;
-- works
-- Test 2
select * from -- comment
dual;
/* works, 'Query Result SQL' shows (note this is 2 lines)
select * from -- comment
dual
*/
-- Test 3
select * from --a
dual;
/* works, 'Query Result SQL' shows (note this is 2 lines)
select * from --a
dual
*/
-- Test 4
select * from --
dual;
/* does not work, ORA-00903: invalid table name, 'Query Result SQL' shows (note this is 1 line)
select * from - dual
*/
-- Test 5
select * from ---
dual;
/* does not work, ORA-00903: invalid table name, 'Query Result SQL' shows (note this is 1 line)
select * from -- dual
*/
As per the 10g documentation that I could find, both -- and --- should be valid https://docs.oracle.com/cd/B13789_01/server.101/b10759/sql_elements006.htm:
Begin the comment with -- (two hyphens). Proceed with the text of the
comment. This text cannot extend to a new line. End the comment with a
line break.

see comments. this is a bug in oracle sql developer.

Related

"select ;" why does this statement exist in postgresql

The query:
select ;
is valid in postgresql. It returns a tuple with no attribute.
# select ;
--
(1 row)
It has clear semantics and the result can be used as a subquery:
# select 1 from (select ) as rip;
?column?
----------
1
(1 row)
In fact, one can create a table with no attributes using it. One can even add tuples to it!!
But my question is, why does it exist?
I see value in a select without a from clause, as psql can be used as a calculator:
# select 3 * 6;
?column?
----------
18
(1 row)
and or be used to call a UDF.
But I can not envision a use for select ;
it is useful or is it an oddity of postgresql's parser?
See here:
https://www.postgresql.org/docs/current/sql-select.html
Empty SELECT Lists
The list of output expressions after SELECT can be empty, producing a zero-column result table. This is not valid syntax according to the SQL standard. PostgreSQL allows it to be consistent with allowing zero-column tables. However, an empty list is not allowed when DISTINCT is used.

append an id onto the name of a table

I have a select statement
select * from schema.table_id
where ID is a number that is passed into a function
how do I append this ID onto the table I have tried || but that gave a syntax error. I have also tried using concat('schema.table_, id::text) but when that ran a select * from %I, table name that gave an error of relation "schema.tablename" does not exist I assume this is due to it being in quotes
This answer is limited to you problem mentioned in the question. You should write it in this way:
format('select * from %I.%I',schema_name,'table_'||id::text)
or
format('select * from "schema_name".%I','table_'||id::text)

SSRS 2005 passing parameters to SQL Server 2000 stored procedure

Below is code that I built from an example I found online, I can't find the link, but the code is referenced in the answers on this stack overflow question: Passing multiple values for a single parameter in Reporting Services.
Here is the SQL code I am working with right now within my stored procedure, it was a long procedure so I summed it down to just the section I am working on, and added the DECLARE and SET for #EMPLOYEES, which are passed as a parameter from SSRS to make the code snippet run.
DECLARE #EMPLOYEES varchar(8000)
-- EMPLOYEES is a comma separated list of EMPLOYEE IDS
-- FROM SSRS Report Parameters. Each ID is 12 characters
-- And there are 806 Employees to choose from, which
-- when all are selected, the Comma separated string grows
-- to 11,193 characters, much longer than 8000
SET #EMPLOYEES = 'EMP000000001,EMP000000002,EMP000000003'
CREATE TABLE #EMPLOYEEIDS
(
EMPLOYEEID varchar(100) NOT NULL
)
DECLARE #CharIndex AS int
DECLARE #Piece AS varchar(100)
-- FILL THE #EMPLOYEEIDS TABLE WITH THE COMMA SEPARATED EMPLOYEE IDS
SELECT #CharIndex = 1
WHILE #CharIndex > 0 AND LEN(#EMPLOYEES) > 0
BEGIN
SELECT #CharIndex = CHARINDEX(',', #EMPLOYEES)
IF #CharIndex > 0
SELECT #Piece = LEFT(#EMPLOYEES, #CharIndex - 1)
ELSE
SELECT #Piece = #EMPLOYEES
INSERT INTO #EMPLOYEEIDS (EMPLOYEEID) VALUES (#Piece)
SELECT #EMPLOYEES = RIGHT(#EMPLOYEES, LEN(#EMPLOYEES) - #CharIndex)
END
SELECT * FROM #EMPLOYEEIDS
DROP TABLE #EMPLOYEEIDS
I had 6 sets of multi-values, all of them worked fine, until I found that the reports were missing much of the data for employees, to which I found that the VARCHAR(8000) was overflowed when selecting all the employees on the report parameters (there are over 800 of them). The Report would run, SQL would happily truncate the VARCHAR to 8000 characters, and a quarter of the IDS were not parsed.
So I tried to switch the VARCHAR to a text field, and none of the parsing functions would work when the field is set up as TEXT. I get errors like the following:
Msg 8116, Level 16, State 2, Procedure usp_QualityMonitoring_AllProfiles_SelectWithParameters, Line 89
Argument data type text is invalid for argument 1 of left function.
This is understandable, I know that many functions that work with VARCHAR will not work with TEXT. So, SQL is truncating everything after 8000 characters when I use a VARCHAR, and the procedure won't ever run if I switch it to TEXT.
What other options to I have to pass multi-valued parameters from SSRS to a SQL Server stored procedure that can support this many options?
OR is there a way to fix the code in the stored procedure to parse through TEXT instead of VARCHAR?
Note: I originally thought the SQL Server running the Stored Proc was 2005, but I have determined that it is not:
SELECT ##VERSION
-- Microsoft SQL Server 2000 - 8.00.2039 (Intel X86) May 3 2005 23:18:38 Copyright (c) 1988-2003 Microsoft Corporation Standard Edition on Windows NT 5.2 (Build 3790: Service Pack 2)

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')

"LIKE" does not work as expected

In Postgresql 8 why this is ok
select * from prod where code like '1%'
select * from prod where code like '%1'
but this returns 0 rows (there are codes begining/ending with digit 1)
select * from prod where code like '1%1'
Update
That happens in my current instalation:
# psql --version
psql (PostgreSQL) 8.3.7
create table a(code char(10));
CREATE TABLE
db=# insert into a values('111');
INSERT 0 1
db=# select * from a where code like '1%';
code
------------
111
(1 row)
db=# select * from a where code like '%1';
code
------
(0 rows)
db=# select * from a where code like '1%1';
code
------
(0 rows)
Update 2
It is the datatype ! With varchar it is Ok !
Thank you.
Is it because the datatype is char(10)?
This means that it will always occupy 10 characters even though you just insert something shorter like "111". Therefore, if you don't use a 10-characters string with "1" at the end, "%1" and "1%1" will never match.
(EDIT: I had posted the following (with with an AND operator, rather than OR).
SELECT * FROM prod WHERE code LIKE '%1' OR code LIKE '1%';
If you want AND operator, the query in the question should work OK. However, if you want to use OR operator, then my above query is probably one of the better ways of doing it.