Solved

Sql query to Stored Procedure

Posted on 2016-11-29
6
51 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 49

Accepted Solution

by:
Vitor Montalvão earned 250 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 28

Assisted Solution

by:Pawan Kumar
Pawan Kumar earned 250 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 28

Expert Comment

by:Pawan Kumar
ID: 41905606
Got it. It is Varchar only. Its the same query I wrote yesterday. :)
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

Author Comment

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

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
I have a large data set and a SSIS package. How can I load this file in multi threading?
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.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

733 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