Solved

SQL stored procedure

Posted on 2014-04-14
6
288 Views
Last Modified: 2014-04-14
Hi,
  I am trying to save this Stored Procedure into the Object Tree. When I execute it stops with the following  error. What I am doing wrong here?

use rpr_dev
/****** Object:  StoredProcedure [dbo].[spITS_DestructionHistory]    Script Date: 04/14/2014 14:15:34 ******/
SET ANSI_NULLS OFF

GO
SET QUOTED_IDENTIFIER OFF
GO

create procedure dbo.spITS_DestructionHistory

-- ALTER   Procedure [dbo].[spITS_DestructionHistory]
--      @AsOfDate datetime,                    -- Date to start looking for
--      @filterString nvarchar(1000)            -- company codes to filter on       
-- AS
      SET NOCOUNT ON

      select
            --item.item_code,
            --CASE
            --      WHEN item.warehouse_id is null THEN item.item_code
            --      ELSE item.warehouse_id
            --END itemcode,
            --ltrim(rtrim( item.item_desc)) as item_desc,
            --item.cust_id,
            --abs(transactions.qty) as Qty,
            item.dest_date,
            item.dest_date_end,
            item.item_code,
            item.id_item
            --company.comp_name
      from item WITH (NOLOCK)
            --left join transactions WITH (NOLOCK) on item.id_item = transactions.id_item and transactions.type = '03'
            --left join company WITH (NOLOCK) on company.comp_code = item.comp_code
      where destroy = 'Y'
            and dest_date is Not Null
            and dest_date_end is Not Null
      -- and PATINDEX('%;' + ltrim(rtrim(item.comp_code)) + ';%', @filterString) > 0
      -- AND item.dest_date > @AsOfDate
      order by item.comp_code, item.dest_date, item.item_code
0
Comment
Question by:tesla764
  • 3
  • 3
6 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39999948
>When I execute it stops with the following  error.
Define 'the following error', as I don't see an error message here.
0
 

Author Comment

by:tesla764
ID: 39999955
The error is...
Msg 111, Level 15, State 1, Line 3
'CREATE PROCEDURE' must be the first statement in a query batch.
0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 39999957
Two things:
create procedure dbo.spITS_DestructionHistory needs to have an AS after it.
SET NOCOUNT ON goes before the CREATE PROC, not after.
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

Author Comment

by:tesla764
ID: 39999969
Is this correct?



use rpr_dev

      SET NOCOUNT ON

create procedure dbo.spITS_DestructionHistory  AS

SET ANSI_NULLS OFF

GO
SET QUOTED_IDENTIFIER OFF
GO
0
 

Author Comment

by:tesla764
ID: 39999983
I think I got it now. Once I commected out the Use statement it worked.

Thanks for your help.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40000008
>Is this correct?
Nope.

Whenever you see a GO, it tells T-SQL to execute everything above it.
So CREATE PROC whatever, then GO, would result in either an error, or an empty proc.

Thanks for the grade.  Good luck with your project.  -Jim
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
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 setup several different housekeeping processes for a SQL Server.

840 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