Solved

SQL stored procedure

Posted on 2014-04-14
6
287 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

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.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

816 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

8 Experts available now in Live!

Get 1:1 Help Now