converting sql query into a stored proc - stored-procedures

Select OpportunityId
from opportunity AS c
left JOIN (
select a.opportunitynameid
from opportunity o
JOIN ApprovalDocument a ON a.opportunitynameid=o.OpportunityId
) AS b ON c.OpportunityId=b.opportunitynameid
Where b.opportunitynameid IS NULL and statecode=0

Create Procedure mySP_GetOpportunity
#statuscode int = 0
As
Select OpportunityId
from opportunity AS c left JOIN
( select a.opportunitynameid from opportunity o JOIN ApprovalDocument a ON a.opportunitynameid=o.OpportunityId ) AS b
ON c.OpportunityId=b.opportunitynameid
Where b.opportunitynameid IS NULL
and statecode=#statuscode

Wrap the sql query between
CREATE PROCEDURE Proc_Name
AS
"query goes here"
GO
for more detail refer this

Related

Snowflake query with column alias in join failing

This query works in one account but throws an error (below) in another. Is there a parameter that affects this behaviour? Both accounts have the same Snowflake version (6.41.2).
Error Message
with cte_one as (
select 'aaa' a
,'bbb' b
)
select t1.a || t1.b as c
from cte_one t1
join cte_one t2 on c = t2.a || t2.b
;
This will work.
with cte_one as (
select 'aaa' a
,'bbb' b
), t3 as
(
select t1.a || t1.b as c
from cte_one t1
)
select * from t3
join cte_one t2 on c = t2.a || t2.b
;
C
A
B
aaabbb
aaa
bbb
Virtual column C doesn't exist yet in the original syntax when the compiler goes into the join. As humans we can read what the original SQL is trying to do and it makes sense. The compiler sees it differently.
Out of curiosity I went to SQL Fiddle I tried the original SQL on Oracle, Postgres, and MS SQL Server. They all reported errors saying C does not exist.
The provided code sample works as-is:
Environment:
SELECT CURRENT_REGION(), CURRENT_VERSION();
-- AZURE_WESTEUROPE 6.41.2
Query profile:
explain using tabular
with cte_one as (
select 'aaa' a ,'bbb' b union all select 'x', 'y'
)
select t1.a || t1.b as c, 1
from cte_one t1
join cte_one t2
on c = t2.a || t2.b;
Output:
There is a feature for resolving aliases from the select list in the join predicate. The rollout process was not completed yet for all accounts. You should engage Snowflake Support and ask them to fix the configuration discrepancies on your accounts.
I would suggest disabling it on both accounts and using Greg's workaround.

How to get foreign key value from Stored Procedure while showing the list.?

Following is the procedure that i have written in my application. But i want the LogSource Column in my list, but i am not able to get it from this stored procedure.
CREATE PROCEDURE [dbo].[GetApplicationLogs]
-- Add the parameters for the stored procedure here
#Skip int,
#Pagesize int
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
WITH TableDatawithRowNumber AS
( SELECT dbo.ApplicationLog.* ,ROW_NUMBER() OVER (ORDER BY LoggedDate DESC) AS RowNumber,
(SELECT COUNT(*) AS Expr1
FROM ApplicationLog ) AS TotalRecords
from ApplicationLog
)
SELECT * FROM TableDatawithRowNumber
WHERE RowNumber > #Skip AND RowNumber <= (#Pagesize+#Skip)
END
This table doensn't contain a LogSource column but it is having LogSourceID in it which is a foreign key in this table and the primary key in LogSource Table. I want to show that in my list but i am not able to get it in the view. I can only use LogSourceId but not LogSource. So please help me.
I think you just need to do a left join on the LogSource table
WITH TableDatawithRowNumber AS
( SELECT dbo.ApplicationLog.* ,ROW_NUMBER() OVER (ORDER BY LoggedDate DESC) AS RowNumber, logSource.LogSource
(SELECT COUNT(*) AS Expr1
FROM ApplicationLog ) AS TotalRecords
from ApplicationLog as appLog
left join LogSource as logSource on appLog.LogSourceId = logSource.LogSourceId
)

How to get return rowcount from stored proceedure - SQL Server 2012

I am writing a stored procedure in SQL Server 2012 and facing problem while reading the number of rows that the stored procedure will return after matching all the conditions and join criteria.
My stored procedure is:
SELECT DISTINCT
COUNT(crs.CourseId) OVER() AS Recordcounts,
crs.CourseId,
crs.CourseName,
crs.CourseDescription,
(SELECT CourseGroupName FROM CourseGroup cgrp
WHERE cgrp.CourseGroupId = crs.CourseGroupId) AS Category
FROM
Courses crs
INNER JOIN
CourseRequests creq ON crs.CourseId = creq.CourseId
WHERE
crs.Coursename <> ''''
It is returning 16 as "Recordcounts" for one of condition, but in actual, the result is 3 rows only.
Can anybody help me with this?
Thanks
Below screenshot will give more clear idea about problem for one of condition:
Try this:
;with cte as(
SELECT distinct
crs.CourseId,
crs.CourseName,
crs.CourseDescription,
(SELECT CourseGroupName FROM CourseGroup cgrp
WHERE cgrp.CourseGroupId = crs.CourseGroupId) AS Category
FROM
Courses crs
INNER JOIN
CourseRequests creq ON crs.CourseId = creq.CourseId
WHERE
crs.Coursename <> '''')
Select *, (select COUNT(CourseId) from cte) AS Recordcounts
from cte

Duplicate values returned on SQL INNER JOIN

I'm getting duplicate values returned when I do an inner join to the change_management table. it returns three records but I only want the most recent cmp.id.
SELECT
cmp.id,
cr.id,
coalesce(cmp.effort, 0.00) AS "Effort"
FROM
m_change_request cr
INNER JOIN (select max(id) as id, change_request_fk, effort from m_change_management group by id, change_request_fk, effort) as cmp ON cmp.change_request_fk = cr.id
WHERE
cr.release_fk=509
I need it to return the most recent record by max(cmd.id). Any ideas how I can fix this ?
Found the solution
SELECT
cmp.id,
cr.id,
cr.number AS "PSL #"
FROM
m_change_request cr
LEFT JOIN m_change_management cmp ON cr.id = cmp.change_request_fk
LEFT JOIN m_change_management cmp2 ON cr.id = cmp2.change_request_fk AND cmp.id < cmp2.id
WHERE
cr.release_fk=509 AND cmp2.change_request_fk IS NULL

Error in Hive Query while joining tables

I am unable to pass the equality check using the below HIVE query.
I have 3 table and i want to join these table. I trying as below, but get error :
FAILED: Error in semantic analysis: Line 3:40 Both left and right aliases encountered in JOIN 'visit_date'
select t1.*, t99.* from table1 t1 JOIN
(select v3.*, t3.* from table2 v3 JOIN table3 t3 ON
( v3.AS_upc= t3.upc_no AND v3.start_dt <= t3.visit_date AND v3.end_dt >= t3.visit_date AND v3.adv_price <= t3.comp_price ) ) t99 ON
(t1.comp_store_id = t99.cpnumber AND t1.AS_store_nbr = t99.store_no);
EDITED based on help from FuzzyTree:
1st:
We tried to edit above query using between and where clause, but not getting any output from the query.
But If we changed the above query by removing the between clause with date, then I got some output based on "v3.adv_price <= t3.comp_price", but not using "date filter".
select t1.*, t99.* from table1 t1 JOIN
(select v3.*, t3.* from table2 v3 JOIN table3 t3 on (v3.AS_upc= t3.upc_no)
where v3.adv_price <= t3.comp_price
) t99 ON
(t1.comp_store_id = t99.cpnumber AND t1.AS_store_nbr = t99.store_no);
2nd :
Next we tried to pass only one date as :
select t1.*, t99.* from table1 t1 JOIN
(select v3.*, t3.* from table2 v3 JOIN table3 t3 on (v3.AS_upc= t3.upc_no)
where v3.adv_price <= t3.comp_price and v3.start_dt <= t3.visit_date
) t99 ON
(t1.comp_store_id = t99.cpnumber AND t1.AS_store_nbr = t99.store_no);
So, now it's showing some result but if we pass both the start and end date filter, it; not showing any result.
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Joins
Only equality joins, outer joins, and left semi joins are supported in
Hive. Hive does not support join conditions that are not equality
conditions as it is very difficult to express such conditions as a
map/reduce job.
Try moving your inequalities to the where clause
select t1.*, t99.* from table1 t1 JOIN
(select v3.*, t3.* from table2 v3 JOIN table3 t3 on (v3.AS_upc= t3.upc_no)
where t3.visit_date between v3.start_dt and v3.end_dt
and v3.adv_price <= t3.comp_price
) t99 ON
(t1.comp_store_id = t99.cpnumber AND t1.AS_store_nbr = t99.store_no);

Resources