?
Solved

Sql query to Stored Procedure

Posted on 2016-11-29
6
Medium Priority
?
60 Views
Last Modified: 2016-11-29
Hello,

I have a query need to convert it to SP:

Any suggestions:


SELECT TOP 1 CASE WHEN cnt = cnt1 THEN 1 ELSE 0 END IsCheckedAll  
FROM (
      SELECT * , COUNT(*) OVER () cnt , COUNT(Checked) OVER () cnt1 FROM PRXTRACT
      WHERE InvoiceNumber =@InvoiceNumber
)k

Open in new window

0
Comment
Question by:RIAS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 51

Accepted Solution

by:
Vitor Montalvão earned 1000 total points
ID: 41905601
Basically write the CREATE PROCEDURE header with the necessary parameter definition and copy the query into the SP:
CREATE PROCEDURE  usp_Name
   @InvoiceNumber INT      
AS
BEGIN
	SELECT TOP 1 CASE WHEN cnt = cnt1 THEN 1 ELSE 0 END IsCheckedAll  
	FROM (
      SELECT * , COUNT(*) OVER () cnt , COUNT(Checked) OVER () cnt1 FROM PRXTRACT
      WHERE InvoiceNumber =@InvoiceNumber
		) k

END

Open in new window

0
 
LVL 29

Assisted Solution

by:Pawan Kumar
Pawan Kumar earned 1000 total points
ID: 41905603
Try.. choose Datatype based on your column name, Are you expecting NULL also in @InvoiceNumber ?

CREATE PROC YourProcName
(
	@InvoiceNumber VARCHAR(100) 
)
AS
BEGIN


SELECT TOP 1 CASE WHEN cnt = cnt1 THEN 1 ELSE 0 END IsCheckedAll  
FROM (
      SELECT * , COUNT(*) OVER () cnt , COUNT(Checked) OVER () cnt1 FROM PRXTRACT
      WHERE InvoiceNumber =@InvoiceNumber
)k


END

Open in new window

0
 
LVL 29

Expert Comment

by:Pawan Kumar
ID: 41905606
Got it. It is Varchar only. Its the same query I wrote yesterday. :)
0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 

Author Comment

by:RIAS
ID: 41905619
Experts,Both the solution are same except datatype.
Will split points equally.
0
 
LVL 35

Expert Comment

by:ste5an
ID: 41905631
This should be faster:

IF EXISTS ( SELECT  *
            FROM    PRXTRACT
            WHERE   InvoiceNumber = @InvoiceNumber
                    AND Checked IS NULL )
    SELECT  0 AS IsCheckedAll;    
ELSE
    SELECT  1 AS IsCheckedAll;

Open in new window

1
 

Author Comment

by:RIAS
ID: 41905680
Thanks Stefan!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question