Solved

SQL stored procedure

Posted on 2014-04-14
6
285 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

762 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now