I am trying this statement:
Declare #Param1 int = 0
SELECT #Param1,T.N
CROSS JOIN (VALUES(0),(1),(2)) as T(N)
Returns this error:
Msg 156, Level 15, State 1
Incorrect syntax near the keyword 'CROSS'.
It seems I can only use this Cross Join when I use FROM first, but not by itself?
I guess when you do it like this it works:
SELECT #Param1,T.N
FROM (VALUES(0),(1),(2)) as T(N)
Yes, you cannot do a join outside of a from statement. Use a table var for param and join it?
Declare #Param1 table(param1 int);
insert into #param1 values(0)
SELECT *
from #Param1
CROSS JOIN (VALUES(0),(1),(2)) as T(N);
Related
I am trying to write a query where I can have either of the 2 variables entered and the query needs to execute on the basis of which data was entered. For example
DECLARE #A NVARCHAR(15) = '' -- Input A here
-------------------OR---------------------
DECLARE #B NVARCHAR(15) = '' -- Input B here
SELECT
[DOC].[ety_ts] as [CreatedDate],
[DOC].[ord_int_id],
[DOC].[doc_int_id],
[pnt_cln_doc_int_id] as [ParentDoc],
CDTL_STATUS2.cod_dtl_ds as DocumentName,
CDTL_STATUS.[cod_dtl_ds] as [DocumentStatus],
.................................
..................................
.....................................
WHERE
IF ( #A IS NULL OR #A = '' )
BEGIN
DOC.asn_int_id = (Select asn_int_id from TPM(nolock) where rec_no=#B)
END
ELSE
BEGIN
DOC.ord_int_id IN
(
SELECT DOC.ord_int_id
FROM TAD_DOCUMENT WITH (NOLOCK)
WHERE DOC.ord_int_id IN
(
SELECT TPM.ord_int_id FROM TPM_VISIT TPM WITH (NOLOCK)
WHERE TPM300.vst_no = #A
)
END
It throws an error for the first IF Statement
Msg 156, Level 15, State 1, Line 59
Incorrect syntax near the keyword 'IF'.
Not sure what I am doing wrong. Can anyone please guide on how to fix this query.
Thanks in advance.
IF is a procedural statement, not a query clause. From the documentation:
An IF...ELSE construct can be used in batches, in stored procedures, and in ad hoc queries.
You just want regular AND/OR logic e.g.
WHERE (
NULLIF(#A,'') IS NULL
AND DOC.asn_int_id = (
SELECT asn_int_id
FROM TPM
WHERE rec_no = #B
)
)
OR (
NULLIF(#A,'') IS NOT NULL
AND DOC.ord_int_id IN (
SELECT DOC.ord_int_id
FROM TAD_DOCUMENT
WHERE DOC.ord_int_id IN (
SELECT TPM.ord_int_id
FROM TPM_VISIT TPM
WHERE TPM300.vst_no = #A
)
)
)
Note: Unless you are very sure you understand and accept the consequences of using NOLOCK I highly recommend not using it. Its not a "go faster for free" hint.
I need this query inside a select statement in another query, i have tried to put it inside a function and call it from the select, but i get errors maybe because non-scalar statements are not allowed in a function. How can i proceed?
SELECT Tip.NombreTipoMov
INTO #tabla_temp
FROM ut_sgt_Movimientos_t Reg
INNER JOIN ut_sgt_TiposMovimientos_m Tip
ON Reg.id_TipoMov = Tip.id_TipoMov
WHERE Reg.id_Registro = #IdRegistro
DECLARE #data VARCHAR(100)
UPDATE #tabla_temp
SET #data = #data + ' ' + NombreTipoMov
SELECT LTRIM(RTRIM(#data))
DROP TABLE #tabla_temp
This is the function:
CREATE FUNCTION dbo.uf_sgt_ObtieneTiposMovimientosXRegistroVehiculo
(
#IdRegistro INTEGER
)
RETURNS VARCHAR(100)
AS
BEGIN
DECLARE #Retorno VARCHAR(100)
SET #Retorno = ( SELECT Tip.NombreTipoMov
INTO #tabla_temp
FROM ut_sgt_Movimientos_t Reg
INNER JOIN ut_sgt_TiposMovimientos_m Tip
ON Reg.id_TipoMov = Tip.id_TipoMov
WHERE Reg.id_Registro = #IdRegistro
DECLARE #data VARCHAR(100)
UPDATE #tabla_temp
SET #data = #data + ' ' + NombreTipoMov
SELECT LTRIM(RTRIM(#data))
DROP TABLE #tabla_temp
)
RETURN #Retorno END GO
This is the error:
Incorrect syntax near the keyword 'INTO'.
Illegal UPDATE statement within a scalar SQL function.
Illegal SELECT statement within a scalar SQL function.
Illegal DROP TABLE statement within a scalar SQL function.
Incorrect syntax near ')'.
I am trying to do a select like this -
SELECT COUNT(*)
FROM
(
DECLARE #Emp varchar(100)
SET #Emp = 'Rick'
SELECT *
FROM [Temporary].[dbo].[Employee_Test]
WHERE [Emp_Name] = #Emp
) AS EMPS
I know that I can fix this by putting the DECLARE statements before outer select. But, due to some crappy design, I would prefer to be able to use the inner query as is and then count the number of rows in it. How do I do that ?
The error I get is -
Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'DECLARE'.
Msg 102, Level 15, State 1, Line 9
Incorrect syntax near ')'
DECLARE #Emp varchar(100)
SET #Emp = 'Rick';
SELECT COUNT(*)
FROM [Temporary].[dbo].[Employee_Test]
WHERE [Emp_Name] = #Emp
You need to Declare The variable and SET its values before you pass it to the SELECT Statement. You cannot Declare a Variable Inside a Select Statement.
Should you put a semi-colon after the SET statement?
SET #EMP = 'Rick';
I have a rather complicated CTE that I'm attempting to incorporate into a stored procedure. It works when just operating straight from SQL Server Management Studio. When I try to create my stored procedure, I get an error:
Msg 102, Level 15, State 1, Procedure spMyCrazyProc, Line 56
Incorrect syntax near ','.
What have I syntactically done incorrectly when trying to incorporate my CTE into a stored procedure?
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[spMyCrazyProc]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[spMyCrazyProc]
GO
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE dbo.spMyCrazyProc
#CompanyId Int,
#EmployeeIds varchar(MAX)
AS
;with SelectedEmployees as (
select * from vwEmployee e
where e.CompanyId = #CompanyId
and (#EmployeeIds is null or #EmployeeIds='' or exists(select ce.SelectedEmployeeId from #myTmpTable ce where ce.SelectedEmployeeId=e.EmployeeId)
), MyStuffA as (
select * from SelectedEmployees
)
select * from MyStuffA
GO
Using sensible coding conventions and thinking about readability (indenting, carriage returns) would have yielded this simple error much more clearly. Your code with 500 character-wide lines removed:
;with SelectedEmployees as
(
select * from vwEmployee e
where e.CompanyId = #CompanyId
and
(
#EmployeeIds is null or #EmployeeIds='' or exists
(
select ce.SelectedEmployeeId from #myTmpTable ce
where ce.SelectedEmployeeId=e.EmployeeId
)
----^----- oops! Missing closing paren here.
), MyStuffA as
(
select * from SelectedEmployees
)
select * from MyStuffA
I have a tree-style database with the following structure:
Table fields:
NodeID int
ParentID int
Name varchar(40)
TreeLevel int
I would like to use a variable #NodeID in the first part of the with clause to don't get all the table just start from the piece I'm interested in (see where Parent=#ParentID and comment).
with RecursionTest (NodeID,ParentID,ThemeName)
as
(
--if i remove the where from here it spends too much time (the tree is big)--
select Nodeid,ParentID,Name from TreeTable where ParentID=#ParentID
union all
select T0.Nodeid,
T0.ParentID,
T0.Name
from
TreeTable T0
inner join RecursionTest as R on T0.ParentID = R.NodeID
)
select * from RecursionTest
This throws some errors, but my question is:
Is possible to pass a variable to a with clause ?
Thanks a lot in advance.
Best regards.
Jose
Yes.
declare #ParentID int
set #ParentID = 10;
with RecursionTest (NodeID,ParentID,ThemeName) ....
You could wrap the whole thing up in a parameterised inline TVF as well. Example of this last approach.
CREATE FUNCTION dbo.RecursionTest (#ParentId INT)
RETURNS TABLE
AS
RETURN
(
WITH RecursionTest (NodeID,ParentID,ThemeName)
AS
(
/*... CTE definition goes here*/
)
SELECT NodeID,ParentID,ThemeName
FROM RecursionTest
)
GO
SELECT NodeID,ParentID,ThemeName
FROM dbo.RecursionTest(10)
OPTION (MAXRECURSION 0)
Unfortunately <11g this will throw an ORA-32033 - unsupported column aliasing, as this functionality is not supported < that version