• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 70
  • Last Modified:

Sql query to Stored Procedure

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
RIAS
Asked:
RIAS
2 Solutions
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
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
 
Pawan KumarDatabase ExpertCommented:
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
 
Pawan KumarDatabase ExpertCommented:
Got it. It is Varchar only. Its the same query I wrote yesterday. :)
0
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
RIASAuthor Commented:
Experts,Both the solution are same except datatype.
Will split points equally.
0
 
ste5anSenior DeveloperCommented:
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
 
RIASAuthor Commented:
Thanks Stefan!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now