?
Solved

SQL stored procedure

Posted on 2014-04-14
6
Medium Priority
?
293 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 66

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 66

Accepted Solution

by:
Jim Horn earned 2000 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

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 66

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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
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 set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Suggested Courses

752 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