[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 447
  • Last Modified:

Executing a Stored Procedure in the SQL Query Analyzer

To test a stored procedure, what modifications do I need to make to the following stored procedure to run it in the SQL Query Analyzer ?

For instance, if I remove the 1st line, CREATE PROCEDURE dbo.procFlACS, I'll get the message:

Must declare the variable '@RptYear'




CREATE PROCEDURE dbo.procFlACS

@RptYear int
AS

DECLARE @DateFI AS DATETIME
DECLARE @DateMF AS DATETIME
DECLARE @DateC AS DATETIME
DECLARE @DateE AS DATETIME
DECLARE @DateI AS DATETIME

SELECT @DateFI = CAST((@RptYear - tblStatesAll.BondsFS) AS VARCHAR) + '-06-30' from tblStatesAll
SELECT @DateMF = CAST((@RptYear - tblStatesAll.MutualFS) AS VARCHAR) + '-06-30' from tblStatesAll
SELECT @DateC = CAST((@RptYear - tblStatesAll.CashFS) AS VARCHAR) + '-06-30' from tblStatesAll
SELECT @DateE = CAST((@RptYear - tblStatesAll.StocksFS) AS VARCHAR) + '-06-30' from tblStatesAll
SELECT @DateI = CAST(((@RptYear - tblStatesAll.IRAFS)+71) AS VARCHAR) + '-12-31' from tblStatesAll

SELECT Null AS Title, UPPER(tblCustomers.FirstName) AS [First Name], UPPER(tblCustomers.MiddleInitial) AS [MI],
UPPER(tblCustomers.LastName) AS [Last Name], UPPER(tblCustomers.SecondNameFirst) AS [Second Name First],
UPPER(tblCustomers.SecondNameMid) AS [Second Name Mid], UPPER(tblCustomers.SecondNameLast) AS [Second Name Last],
Null AS Relation, UPPER(tblCustomers.Address1) AS [Address 1], UPPER (tblCustomers.Address2) AS [Address 2],
UPPER(tblCustomers.Address3) AS [Address 3], UPPER(tblCustomers.Address4) AS [Address 4], UPPER(tblCustomers.City) AS City,
UPPER(tblCustomers.State) AS State, tblCustomers.Zip AS Zip, tblCustomers.SSN, Null AS [DDA Number], Null AS [Check/Cert Number],
tblProducts.CUSIP, tblProducts.SecurityName AS [Security Name], Null AS [Sub-Issue],
Right(tblCustomers.OfficeNumber,3) + ' ' + tblCustomers.CustomerNumber AS [Account Number],
tblProducts.PropertyType AS [Property Type], Null AS [Property Status],
CASE WHEN tblProducts.CashBalance IS NULL THEN tblProducts.Quantity ELSE tblProducts.CashBalance END AS [Check/Cert Amount]
 
FROM tblStatesAll INNER JOIN (tblCustomers INNER JOIN tblProducts ON tblCustomers.CustomerNumber = tblProducts.CustomerNumber)
ON tblStatesAll.StateFS = tblCustomers.State
WHERE ((tblProducts.PropertyType='OTHER' AND tblCustomers.DateLost <= @DateMF AND tblStatesAll.FallCycle= '1'  AND LEN(tblProducts.IRACode) = 0) OR  
               (tblProducts.PropertyType='MUTUAL FUND' AND tblCustomers.DateLost <= @DateMF AND tblStatesAll.FallCycle= '1'  AND LEN(tblProducts.IRACode) = 0) OR
               (tblProducts.PropertyType='CASH' AND tblCustomers.DateLost <= @DateC AND tblStatesAll.FallCycle= '1'  AND LEN(tblProducts.IRACode) = 0) OR
               (tblProducts.PropertyType='EQUITY' AND tblCustomers.DateLost <= @DateE AND tblStatesAll.FallCycle= '1'  AND LEN(tblProducts.IRACode) = 0) OR
               (tblProducts.PropertyType='FIXED INCOME' AND tblCustomers.DateLost <= @DateFI AND tblStatesAll.FallCycle= '1'  AND LEN(tblProducts.IRACode) = 0) OR
               (tblCustomers.DateOfBirth <= @DateI AND tblStatesAll.FallCycle= '1'  AND LEN(tblProducts.IRACode) > 0) OR
               (LEN(tblCustomers.DateOfBirth) = 0 AND tblStatesAll.FallCycle= '1' ) ) AND tblStatesAll.StateFS = 'MA'  
ORDER BY tblCustomers.State
GO
0
zimmer9
Asked:
zimmer9
  • 3
1 Solution
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
This appears to be a question for the SQL Server TA.
0
 
SidFishesCommented:
you don't need to change anything as a sp can run in query analyzer

if it's an existing sp, you can right click and open...then supply the  @RptYear value

or

you can just paste it in the QA


CREATE PROCEDURE dbo.procFlACS

@RptYear int = 1 -- this sets a default so if no value is input you won't error.

AS

blah..
0
 
zimmer9Author Commented:
I tried to Execute the query from within the SQL Query Analyzer after assigning a value to @RptYear and received the following message:

CREATE PROCEDURE dbo.procFlACS

@RptYear int = 2005
AS

Server: Msg 2714, Level 16, State 5, Procedure procFlACS, Line 19
There is already an object named 'procFlACS' in the database.
0
 
SidFishesCommented:
if you a working on an existing procedure you need to use

ALTER PROCEDURE dbo.procFlACS

0
 
SidFishesCommented:
you can also DROP the store procedure (delete it) and then use CREATE

IF EXISTS (SELECT * FROM sysobjects WHERE TYPE = 'P' AND NAME = 'dbo.procFlACS')
     BEGIN
          DROP  dbo.procFlACS
     END

GO

CREATE PROCEDURE dbo.procFlACS

@RptYear int = 2005
AS

0

Featured Post

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.

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