I am new to Sybase IQ and when I try to execute this code, an error is displayed
Could not execute the statement. Syntax error near SELECT on line 1
DECLARE #SQL VARCHAR(500)
SET #SQL='SELECT * FROM SNP_CHECK_TAB A INNER JOIN SELECT * FROM E$_auditemp_trg B ON B.ODI_ORIGIN = A.ORIGIN WHERE B.ODI_SESS_NO =532001 ORDER BY B.ODI_CHECK_DATE DESC'
EXECUTE(#SQL)
Please help me fix this.
Thanks in advance
You have missed parenthesis around second query for Inner Join.
You Have to use like
... INNER JOIN (SELECT * FROM E$_auditemp_trg ) B ...
-- ^ you need to enclose here
Instaed of
INNER JOIN (SELECT * FROM E$_auditemp_trg B ON B.ODI_ORIGIN = A.ORIGIN WHERE
B.ODI_SESS_NO =532001 ORDER BY B.ODI_CHECK_DATE DESC)
-- ^ (not here)
Try like this
DECLARE #SQL VARCHAR(500)
SET #SQL='SELECT * FROM SNP_CHECK_TAB A INNER JOIN (SELECT * FROM E$_auditemp_trg) B ON B.ODI_ORIGIN = A.ORIGIN WHERE B.ODI_SESS_NO =532001 ORDER BY B.ODI_CHECK_DATE DESC'
EXECUTE(#SQL)
Related
I have a scenario where almost all of the tables have issues with the PK value as follows. This results is a database error or the violation of the PK insert.
When using the DBCC CheckIdent it displays an inconsistency between the next value and the current one.
Can anyone have a reason for the mismatch happening on several tables?
Since this database is then replicate, I'm afraid this error will propagate across the environment.
I adapted this script to fix it, but really trying to figure out the root of the problem.
/** Version 3.0 **/
if object_id('tempdb..#temp') is not null
drop table #temp
;
with cte as (
SELECT
distinct
A.TABLE_CATALOG AS CATALOG,
A.TABLE_SCHEMA AS "SCHEMA",
A.TABLE_NAME AS "TABLE",
B.COLUMN_NAME AS "COLUMN",
IDENT_SEED (A.TABLE_NAME) AS Seed,
IDENT_INCR (A.TABLE_NAME) AS Increment,
IDENT_CURRENT (A.TABLE_NAME) AS Curr_Value
, DBPS.row_count AS NumberOfRows
FROM INFORMATION_SCHEMA.TABLES A
inner join INFORMATION_SCHEMA.COLUMNS B on b.TABLE_NAME = a.TABLE_NAME and b.TABLE_SCHEMA = a.TABLE_SCHEMA
inner join sys.identity_columns IC on OBJECT_NAME (IC.object_id) = a.TABLE_NAME
inner join sys.dm_db_partition_stats DBPS ON DBPS.object_id =IC.object_id
inner join sys.indexes as IDX ON DBPS.index_id =IDX.index_id
WHERE A.TABLE_CATALOG = B.TABLE_CATALOG AND
A.TABLE_SCHEMA = B.TABLE_SCHEMA AND
A.TABLE_NAME = B.TABLE_NAME AND
COLUMNPROPERTY (OBJECT_ID (B.TABLE_NAME), B.COLUMN_NAME, 'IsIdentity') = 1 AND
OBJECTPROPERTY (OBJECT_ID (A.TABLE_NAME), 'TableHasIdentity') = 1 AND
A.TABLE_TYPE = 'BASE TABLE'
)
select 'DBCC CHECKIDENT ('''+A.[SCHEMA]+'.'+a.[TABLE]+''', reseed)' command
, ROW_NUMBER() OVER(ORDER BY a.[SCHEMA], a.[TABLE] asc) AS ID
, A.Curr_Value
, a.[TABLE]
into #temp
from cte A
ORDER BY A.[SCHEMA], A.[TABLE]
declare #i int = 1, #count int = (select max(ID) from #temp)
declare #text varchar(max) = ''
select #COUNT= count(1) FROM #temp
WHILE #I <= #COUNT
BEGIN
SET #text = (SELECT command from #temp where ID=#I)
EXEC (#text + ';')
print #text
select Curr_Value OldValue, ident_current([TABLE]) FixValue, [TABLE] from #temp where ID=#I
SET #I = #I + 1
SET #text='';
END
go
maybe someone or something with enough permissions made a mistake by reseeding?
As simple as this:
create table testid (
id int not null identity (1,1) primary key,
data varchar (3)
)
insert into testid (data) values ('abc'),('cde')
DBCC CHECKIDENT ('testid', RESEED, 1)
insert into testid (data) values ('bad')
I am unable to figure out the problem in the below common table expression query. I'm getting an error message
ERROR:42601:syntax error at the end of the input.
Can someone help me to find the problem in the query?
Thank You.
WITH rpt_1 AS
(SELECT count(*) as cnt, count(distinct memberid) as mem_cnt from table1),
rpt_2 AS
(SELECT count(*) as cnt, count(distinct memberid) as mem_cnt from table2);
You're not using the temporary tables that you create using the WITH keyword. The syntax is like this:
with w1 as (select * from sales), w2 as (select * from supplies)
select * from w1; -- You are skipping this part, which is required
I have a table in my tsql datatable:
CREATE TABLE dbo.Test
(
Col nVarChar (50) null
)
GO
And then I executed query:
Select
c.name As Name, ty.name as Type, c.max_length As MaxLenght, c.precision As Precision, c.scale As Scale, c.is_nullable As IsNullable, *
From
sys.schemas s
inner join sys.tables t on s.schema_id = t.schema_id
inner join sys.columns c on t.object_id = c.object_id
inner join sys.types ty on ty.system_type_id = c.system_type_id
Where
s.name LIKE 'dbo' AND t.name LIKE 'Test'
The question is... Why there are Two Rows?!
Check this:
SELECT ROW_NUMBER() OVER(PARTITION BY system_type_id ORDER BY system_type_id)
, *
FROM sys.types;
Check the first column for values >1...
There are few types mapping to the same system_type_id. Some names are just an alias for something else...
UPDATE
This question addresses the same issue...
Query 1:
SET #sql2 = 'insert into TempReport
select ID, max(TransactionTime),0 from ClubTransaction with (nolock)
where ClubcardID in (select ClubcardID from TempCC)
and ClubcardTransaction.OfferID not in (119,120,121)
group by ClubcardID'
exec (#Sql2)
Query 2:
delcare #OfferID varchar(50)
set OfferID='1,112,445,'
SET #sql2 = 'insert into TempReport
select ID, max(TransactionTime),0 from ClubTransaction with (nolock)
where ClubcardID in (select ClubcardID from TempCC)
and ClubcardTransaction.OfferID not in (Select Item From dbo.fnSplit(#OfferID,'','')
group by ClubcardID'
exec (#Sql2)
Query 1 works fine. In query2 I am replacing with an variable de defined where I am passing to the function fnSplit where I split the values with comma separated.
I get an error message Must declare the scalar variable "#OfferID".
Please let me know where is the issue here.
You have to put the value from outside and replace the ' by ":
Query 2:
declare #OfferID varchar(50)
set OfferID='1,112,445,'
SET #sql2 = 'insert into TempReport
select ID, max(TransactionTime),0 from ClubTransaction with (nolock)
where ClubcardID in (select ClubcardID from TempCC)
and ClubcardTransaction.OfferID not in (Select Item From dbo.fnSplit(' + replace(convert(varchar(4000), #OfferID), '''', '''''') + ',"","")
group by ClubcardID'
exec (#Sql2)
Another solution (and a better one) is to use sp_executesql
Be aware that dynamic SQL is a way to make sql injections and you should avoid using it..
in my stored procedure I have a table variable contains rows ID. The are 2 scenarios - that table variable is empty and not.
declare #IDTable as table
(
number NUMERIC(18,0)
)
In the main query, I join that table:
inner join #IDTable tab on (tab.number = csr.id)
BUT:
as we know how inner join works, I need that my query returns some rows:
when #IDTable is empty
OR
return ONLY rows that exist in
#IDTable
I tried also with LEFT join but it doesn't work. Any ideas how to solve it ?
If `#IDTable' is empty then what rows do you return? Do you just ignore the Join on to the table?
I'm not sure I get what you're trying to do but this might be easier.
if (Select Count(*) From #IDTable) == 0
begin
-- do a SELECT that doesn't join on to the #IDTable
end
else
begin
-- do a SELECT that joins on to #IDTable
end
It is not optimal, but it works:
declare #z table
(
id int
)
--insert #z values(2)
select * from somTable n
left join #z z on (z.id = n.id)
where NOT exists(select 1 from #z) or (z.id is not null)