Avoiding creating the same query twice in SQL - sql

I have a pretty much simple and self explanatory SQL statement:
ALTER PROCEDURE [dbo].[sp_getAllDebatesForAlias](#SubjectAlias nchar(30))
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT *
FROM tblDebates
WHERE (SubjectID1 in (SELECT SubjectID FROM tblSubjectAlias WHERE SubjectAlias = #SubjectAlias))
OR (SubjectID2 in (SELECT SubjectID FROM tblSubjectAlias WHERE SubjectAlias = #SubjectAlias)) ;
END
I am certain that there is a way to make this statement more efficient, at least get rid of that multiple creation of the same table in the in section, i.e., the
SELECT SubjectID FROM tblSubjectAlias WHERE SubjectAlias = #SubjectAlias
part.
Any ideas?

Try:
select d.* from tblDebates d
where exists
(select 1
from tblSubjectAlias s
where s.SubjectID in (d.SubjectID1, d.SubjectID2) and
s.SubjectAlias = #SubjectAlias)

SELECT d.*
FROM tblDebates d
inner join tblSubjectAlias s on s.SubjectID in (d.SubjectID1, d.SubjectID2)
where s.SubjectAlias = #SubjectAlias

Related

SQL with clause in stored procedure

Is it possible to define a with clause in a stored procedure and use it in if else statements because I always get an error?
BEGIN
WITH Test (F, A) AS
(
SELECT FM.ID, FM.Name
FROM [Test.Abc] FM
INNER JOIN [Organization] O on O.ABCID = FM.ID
)
IF(#var = 'case1')
BEGIN
SELECT *
FROM Test F
WHERE NOT F.ID = 'someID'
END
I always get an "Incorrect syntax" error before the if statement
If I move the with clause into the if statement it works fine. But I need the with statement outside to reuse it in different if else statements.
Here's another version of the same answers you're getting:
Your with common table expresson has to be in the same statement as the query that calls it, and it has to be referenced by a query (or other cte) or it is a syntax error.
Reference the documentation Guidelines for Creating and Using Common Table Expressions.
BEGIN -- doing stuff
-- .... doing stuff over here
IF(#var = 'case1')
BEGIN
with Test (F, A) as (
select FM.ID, FM.Name from [Test.Abc] FM
inner join [Organization] O on O.ABCID = FM.ID
)
select * from Test F
where not F.ID = 'someID'
END
-- .... and doing some other stuff over here too
END -- done with this stuff
Just use a temporary table or table variable. The scoping rules of SQL Server ensure that such a table is dropped at the end of the procedure:
BEGIN
select FM.ID, FM.Name
into #test
from [Test.Abc] FM inner join
[Organization] O
on O.ABCID = FM.ID;
IF(#var = 'case1')
BEGIN
select *
from #Test F
where not F.ID = 'someID'
END;
This has the advantage that you can add indexes to the table, and these might improve performance.
WITH is not a standalone, it always a part of a whole statement and only one statement.
It is not recognizable outside the scope ofits statement.
BEGIN
with my_cte (n) as (select 1+1)
select * from my_cte
-- The following statement yields the error "Invalid object name 'my_cte'."
-- select * from my_cte
END

sql inner table substitution

Suppose I have an sql query like the following (I realize this query could be written better, just bear with me):
SELECT aT.NAME
FROM anothertable aT,
( SELECT ts.slot_id,
tgm.trans_id,
tagm.agent_id
FROM slots ts,
transactions tgm,
agents tagm
WHERE ts.slot_id = (12345, 678910)
and ts.slot_id = tagm.slot_id
AND ts.slot_id = tgm.slot_id) INNER
WHERE INNER.trans_id = aT.trans_id
AND INNER.agent_id = aT.trans_id
Now suppose that I need to break up this query into two parts...in the first I'll execute the inner query, do some processing on the results in code, and then pass back a reduced set to the outer part of the query. The question is, is there an easy way to emulate an inner table in sql?
For instance, if the results of the inner query returned 5 rows but my program deems to only need two of those rows, how can I write sql that will do what I am trying to do below? Is there a way, in sql, to declare a table for in memory in query use?
SELECT
at.Name
FROM
anotherTable aT,
(SLOT_ID, TRANS_ID, AGENT_ID
-------------------------
230743, 3270893, 2307203
078490, 230897, 237021) inner
WHERE
inner.trans_id = at.trans_id
AND INNER.agent_id = aT.trans_id
Just use a subquery:
SELECT at.Name
FROM anotherTable aT JOIN
(select 230743 as SLOT_ID, 3270893 as TRANS_ID, 2307203 as AGENT_ID from dual
select 078490, 230897, 237021 from dual
) i
on i.trans_id = at.trans_id AND i.agent_id = aT.trans_id;
Most systems will let you define a TEMP TABLE or TABLE VARIABLE: https://www.simple-talk.com/sql/t-sql-programming/temporary-tables-in-sql-server/
CREATE TABLE #temp (
SLOT_ID INT,
TRANS_ID INT,
AGENT_ID INT
);
INSERT INTO #temp(SLOT_ID, TRANS_ID, AGENT_ID)
(--inner query goes here)
--do your main query, then:
DROP TABLE #temp
IN MS SQL Server (not sure about other systems), you could possibly use a Common Table Expression (CTE): https://technet.microsoft.com/en-us/library/ms190766%28v=sql.105%29.aspx
WITH inner AS (
--inner query goes here
)
--main select goes here
Personally, since I generally work with MSSQL Server, I use CTE's quite a bit, as they can be created "on the fly", and can be a big help in organizing more complex queries.
The subquery method worked. Since this is Oracle, the syntax turned out to be:
SELECT aT.Name
FROM anotherTable aT,
(select 1907945 as SLOT_ID, 2732985 as TRANS_ID, 40157 as AGENT_ID FROM DUAL
union
select 1907945, 2732985, 40187 FROM DUAL
) inner
WHERE
inner.trans_id = aT.trans_id AND INNER.agent_id = aT.trans_id;

Output result and update 2nd table without using a temporary table

I currently have the following query
SELECT TOP (#batchSize) * into #t
from iv.SomeView
where EXISTS(select *
from iv.PendingChanges
where SomeView.RecordGuid = PendingChanges .RecordGuid)
update iv.PendingChanges
set IsQueued = 1
where RecordGuid in (select #t.RecordGuid from #t)
select * from #t
This pattern (using different views and PendingChanges tables in the various queries) is run very frequently and I was trying to figure out how to reduce the writes to tempdb due to the write to #t.
I came up with this solution which does have a performance boost when comparing it to the old version in the profiler
select top (0) * into #t from iv.SomeView
insert into #t with (TABLOCK)
output inserted.*
SELECT TOP (#batchSize) *
from iv.SomeView
where EXISTS(select *
from iv.PendingChanges
where SomeView.RecordGuid = PendingChanges.RecordGuid)
update iv.PendingChanges
set IsQueued = 1
where RecordGuid in (select #t.RecordGuid from #t)
But is there any way to do this even better so I can both get the result outputted and update PendingChanges.IsQueued without ever needing to write the result out temporarily to #t?
Important note: I can only have a single select from iv.SomeView because the table is very active and doing multiple SELECT TOP (#batchSize) is not deterministic nor do I have any field I could order by to make it so.
Have you tried code like :
update top (#batchsize) PC
Set IsQueued = 1
output inserted.*
From iv.PendingChanges PC
inner join iv.SomeView SV
on iv.SomeView.RecordGuid = iv.PendingChanges.RecordGuid;

Replacement of "IN" in stored procedure

I have huge store procedure. I only paste small part of this.
My Query:
--declare variable and Select Statement goes here
WHERE ((v.[RoleID] IN (
SELECT [dbo].[aspnet_UsersInRoles].roleid
FROM [dbo].[aspnet_UsersInRoles]
INNER JOIN [dbo].[aspnet_Users]
ON [dbo].[aspnet_Users].userid = [dbo].[aspnet_UsersInRoles].userid
WHERE
[dbo].[aspnet_Users].username = #UserName
UNION ALL
SELECT [RoleId]
FROM dbo.aspnet_roles
WHERE loweredrolename = 'anonymous user')
OR v.username = #UserName))
Query works perfect but its take long time to execute. There is so many condition inside WHERE condition with IN. I think the subquery inside IN take time. Is there any way to optimize this query and replace IN inside WHERE condition. Or suggest me an alternative way to optimize this type of query. Thanks.
Since the output of inner subquery is always the same. You can take that outside and insert those values into a temptable like below and u could just query to the temp table. So each time it doesnt go in a loop executing the same
`create table #temp(roleid int); `
` insert into #temp as (sSELECT [dbo].[aspnet_UsersInRoles].roleid
FROM [dbo].[aspnet_UsersInRoles]
INNER JOIN [dbo].[aspnet_Users]
ON [dbo].[aspnet_Users].userid = [dbo].[aspnet_UsersInRoles].userid
WHERE
[dbo].[aspnet_Users].username = #UserName
UNION ALL
SELECT [RoleId]
FROM dbo.aspnet_roles
WHERE loweredrolename = 'anonymous user');`
WHERE ((v.[RoleID] IN ( select roleid from #temp);
You could make a temp table with the result of the IN clause and join to it. This would break the problem to two much simpler tasks.
according to Orangecrush recommendation
--declare variable and Select Statement goes here
WHERE EXISTS (
SELECT 1
FROM [dbo].[aspnet_UsersInRoles]
INNER JOIN [dbo].[aspnet_Users]
ON [dbo].[aspnet_Users].userid = [dbo].[aspnet_UsersInRoles].userid
WHERE
[dbo].[aspnet_Users].username = #UserName
AND [dbo].[aspnet_UsersInRoles].roleid = v.[RoleID]
UNION ALL
SELECT 1
FROM dbo.aspnet_roles
WHERE loweredrolename = 'anonymous user'
AND dbo.aspnet_roles.roleid = v.[RoleID])
OR v.username = #UserName)

How can I select none duplicate rows with inner join?

My MS SQL Server stored procedure is:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_close]
#DATE NVARCHAR(8)
AS
BEGIN
SELECT appointment_datas.appointment_date
,appointment_datas.appointment_no
,costumer_datas.costumer_name
,appointment_datas.appointment_type
,personel_datas.personel_ADI
FROM [LONI].[dbo].appointment_datas
INNER JOIN [LONI].[dbo].costumer_datas ON appointment_datas.appointment_costumer = costumer_datas.costumer_id
INNER JOIN [LONI].[dbo].personel_datas ON appointment_datas.appointment_personel = personel_datas.personel_id
INNER JOIN [GUONU].[dbo].dayend ON appointment_datas.appointment_no <> dayend.appointment_no COLLATE Turkish_CI_AS
WHERE CONVERT(nvarchar(8),appointment_datas.appointment_date,112) = #DATE
END
With this code, I select duplicate rows from the same records. Actually I want to select fields from [LONI].[dbo].appointment_datas but if appointment_no
is not in [GUONU].[dbo].dayend
SELECT DISTINCT removes duplicates in your output.
But your SQL looks wrong. Are you sure you mean to write:
TABLE1.FIELD1 <> TABLE1.FIELD1
This always evaulates to false. I think you may have an error in your SQL, and that might be why you are getting duplicate values. You should rarely use <> in a join clause, and you shouldn't have the same field on both sides.
Maybe you intended:
ON [DB1].[dbo].TABLE1.FIELD1 <> [DB2].[dbo].TABLE1.FIELD1
but this will generate a Cartesian product of all the rows that don't match. I doubt this is what you really mean. Perhaps you want this:
ON [DB1].[dbo].TABLE1.ID = [DB2].[dbo].TABLE1.ID
WHERE[DB1].[dbo].TABLE1.FIELD1 <> [DB2].[dbo].TABLE1.FIELD1
This matches the rows from each database that have the same ID, but differ in a certain column. Notice that the <> is not in the JOIN clause.
--- UPDATE ---
Perhaps you mean to select the results from the two different databases and then union them?
SELECT appointment_datas.appointment_date
,appointment_datas.appointment_no
,costumer_datas.costumer_name
,appointment_datas.appointment_type
,personel_datas.personel_ADI
FROM [LONI].[dbo].appointment_datas
INNER JOIN [LONI].[dbo].costumer_datas ON appointment_datas.appointment_costumer = costumer_datas.costumer_id
INNER JOIN [LONI].[dbo].personel_datas ON appointment_datas.appointment_personel = personel_datas.personel_id
WHERE CONVERT(nvarchar(8),appointment_datas.appointment_date,112)
UNION
SELECT appointment_datas.appointment_date
,appointment_datas.appointment_no
,costumer_datas.costumer_name
,appointment_datas.appointment_type
,personel_datas.personel_ADI
FROM [GUONU].[dbo].appointment_datas
INNER JOIN [GUONU].[dbo].costumer_datas ON appointment_datas.appointment_costumer = costumer_datas.costumer_id
INNER JOIN [GUONU].[dbo].personel_datas ON appointment_datas.appointment_personel = personel_datas.personel_id
WHERE CONVERT(nvarchar(8),appointment_datas.appointment_date,112)
--- SOLUTION ---
Use NOT EXISTS in WHERE clause. Read comments to see why.
The line
INNER JOIN [DB2].[dbo].TABLE1 ON TABLE1.FIELD1 <> TABLE1.FIELD1
makes no sense, you want to rephrase that...
If I understand your question correctly (after your edit)
but if appointment_no is not in
[GUONU].[dbo].dayend
, you actually want a NOT EXISTS subquery:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_close]
#DATE NVARCHAR(8)
AS
BEGIN
SELECT appointment_datas.appointment_date
,appointment_datas.appointment_no
,costumer_datas.costumer_name
,appointment_datas.appointment_type
,personel_datas.personel_ADI
FROM [LONI].[dbo].appointment_datas
INNER JOIN [LONI].[dbo].costumer_datas ON appointment_datas.appointment_costumer = costumer_datas.costumer_id
INNER JOIN [LONI].[dbo].personel_datas ON appointment_datas.appointment_personel = personel_datas.personel_id
WHERE CONVERT(nvarchar(8),appointment_datas.appointment_date,112) = #DATE
AND NOT EXISTS (SELECT 'X' FROM [GUONU].[dbo].dayend WHERE dayend.appointment_no = appointment_datas.appointment_no)
END
SELECT DISTINCT TABLE1.FIELD1
,TABLE2.FIELD1
,TABLE1.FIELD3
,TABLE3.FIELD1
FROM ...
NB in some variants you will have to bracket the field list ie
SELECT DISTINCT (TABLE1.FIELD1
,TABLE2.FIELD1
,TABLE1.FIELD3
,TABLE3.FIELD1 ) FROM ...