[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

SQL stored procedure

Posted on 2014-04-14
6
Medium Priority
?
305 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

656 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