Solved

Can't Find Error in stored procedure

Posted on 2011-02-25
7
193 Views
Last Modified: 2012-05-11
Hi EE,

I have ran this procedure all day trying to figure out what is wrong.
I get an error that there is an error near 'F'
I have gone through this so many times I thought maybe a pair of fresh eyes can spot it.

Thanks
ALTER PROCEDURE  [dbo].[stp_FindInventory]
	@SupplierID int,
  @Brandid int,
  @ProductTypeid int,
	@UserID uniqueidentifier
  
as
begin

SET FMTONLY OFF;
declare @Hyphen char(2)
declare @sql varchar(8000)
declare @sqlWhere varchar(8000)
Declare @None char(4)
set @None = 'None'

set @Hyphen = '-'

set @sqlWhere = ' dbo.ProductTypesLU = ' + cast(@UserID as varchar(100)) 

if @SupplierID > 0
begin
  set @sqlWhere = @sqlWhere + ' and dbo.ProductsLU.SupplierID = ' + cast(@SupplierID as char(10))
end
if @BrandID > 0
begin
  set @sqlWhere = @sqlWhere + ' and dbo.BrandsLU.BrandID = ' + cast(@BrandID as char(10))
end 
if @ProductTypeID > 0
begin
  set @sqlWhere = @sqlWhere + ' and dbo.ProductsLU.ProductTypeID = ' + cast(@ProductTypeID as char(10))	
end

set @sqlWhere = 'WHERE ' + @sqlWhere 

set @sql = 'SELECT dbo.ProductsLU.LUProdSellUseID, dbo.ProductsLU.UserID, dbo.ProductsLU.SupplierID, dbo.ProductsLU.ProductTypeID, dbo.ProductsLU.ProductName, 
                      dbo.ProductTypesLU.ProductType, dbo.BrandsLU.BrandName, dbo.BrandsLU.BrandID, dbo.Suppliers.SupplierName, ISNULL(dbo.ProductDetails.ItemNumb, ' + @None + ') 
                      AS ItemNumb, ISNULL(dbo.ProductDetails.ItemNumb,' + @None + ') ' + @hyphen + '  dbo.ProductsLU.ProductName AS ItemProduct, ISNULL(dbo.ProductDetails.OuncesInStock, 0) 
                      AS OuncesInStock, ISNULL(dbo.ProductDetails.TotalQty, 0) AS TotalQty, dbo.ProductDetails.Discontinued
					FROM dbo.ProductsLU INNER JOIN
                      dbo.ProductTypesLU ON dbo.ProductsLU.ProductTypeID = dbo.ProductTypesLU.ProductTypeID INNER JOIN
                      dbo.BrandsLU ON dbo.ProductTypesLU.BrandID = dbo.BrandsLU.BrandID INNER JOIN
                      dbo.ProductDetails ON dbo.ProductsLU.LUProdSellUseID = dbo.ProductDetails.LUProdSellUseID INNER JOIN
                      dbo.Suppliers ON dbo.ProductsLU.SupplierID = dbo.Suppliers.SupplierID '





set @sql = @sql + @sqlWhere + 
    ' ORDER BY dbo.Suppliers.SupplierName, dbo.BrandsLU.BrandName, dbo.ProductTypesLU.ProductType, dbo.ProductsLU.ProductName '

exec( @sql )
  
end

Open in new window

0
Comment
Question by:Sheritlw
  • 3
  • 3
7 Comments
 
LVL 15

Expert Comment

by:derekkromm
ID: 34985108
Notice the difference in the first "set @sqlWhere" line w/ the @UseriD parameter

alter PROCEDURE  [dbo].[stp_FindInventory]
	@SupplierID int,
  @Brandid int,
  @ProductTypeid int,
	@UserID uniqueidentifier
  
as
begin

SET FMTONLY OFF;
declare @Hyphen char(2)
declare @sql varchar(8000)
declare @sqlWhere varchar(8000)
Declare @None char(4)
set @None = 'None'

set @Hyphen = '-'

set @sqlWhere = ' dbo.ProductTypesLU = ''' + cast(@UserID as varchar(100)) + ''''

if @SupplierID > 0
begin
  set @sqlWhere = @sqlWhere + ' and dbo.ProductsLU.SupplierID = ' + cast(@SupplierID as char(10))
end
if @BrandID > 0
begin
  set @sqlWhere = @sqlWhere + ' and dbo.BrandsLU.BrandID = ' + cast(@BrandID as char(10))
end 
if @ProductTypeID > 0
begin
  set @sqlWhere = @sqlWhere + ' and dbo.ProductsLU.ProductTypeID = ' + cast(@ProductTypeID as char(10))	
end

set @sqlWhere = 'WHERE ' + @sqlWhere 

set @sql = 'SELECT dbo.ProductsLU.LUProdSellUseID, dbo.ProductsLU.UserID, dbo.ProductsLU.SupplierID, dbo.ProductsLU.ProductTypeID, dbo.ProductsLU.ProductName, 
                      dbo.ProductTypesLU.ProductType, dbo.BrandsLU.BrandName, dbo.BrandsLU.BrandID, dbo.Suppliers.SupplierName, ISNULL(dbo.ProductDetails.ItemNumb, ' + @None + ') 
                      AS ItemNumb, ISNULL(dbo.ProductDetails.ItemNumb,' + @None + ') ' + @hyphen + '  dbo.ProductsLU.ProductName AS ItemProduct, ISNULL(dbo.ProductDetails.OuncesInStock, 0) 
                      AS OuncesInStock, ISNULL(dbo.ProductDetails.TotalQty, 0) AS TotalQty, dbo.ProductDetails.Discontinued
					FROM dbo.ProductsLU INNER JOIN
                      dbo.ProductTypesLU ON dbo.ProductsLU.ProductTypeID = dbo.ProductTypesLU.ProductTypeID INNER JOIN
                      dbo.BrandsLU ON dbo.ProductTypesLU.BrandID = dbo.BrandsLU.BrandID INNER JOIN
                      dbo.ProductDetails ON dbo.ProductsLU.LUProdSellUseID = dbo.ProductDetails.LUProdSellUseID INNER JOIN
                      dbo.Suppliers ON dbo.ProductsLU.SupplierID = dbo.Suppliers.SupplierID '





set @sql = @sql + @sqlWhere + 
    ' ORDER BY dbo.Suppliers.SupplierName, dbo.BrandsLU.BrandName, dbo.ProductTypesLU.ProductType, dbo.ProductsLU.ProductName '

exec( @sql )
  
end

Open in new window

0
 

Author Comment

by:Sheritlw
ID: 34985208
Wow, I can't believe I missed that... thank you.

I changed the line to
set @sqlWhere = ' dbo.ProductTypesLU.UserID = ' + cast(@UserID as varchar(100))

but I still get the same error.

Any other ideas?

Thanks

0
 
LVL 15

Expert Comment

by:derekkromm
ID: 34985220
you still need to wrap wrap the @userID in quotes

the resulting sql would look like this:

"where UserID = abcdefg"

when you actually need it to be "where UserID = 'abcdefg'"

so you need to do

UserId = ''' + cast... + ''''

(those are all single quote marks)
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

Author Comment

by:Sheritlw
ID: 34985329
Sorry, I miss understood.

Now I get the error... "Incorrect syntax near 'dbo'."

I posted my changes.

Thank you so much for your help
ALTER PROCEDURE  [dbo].[stp_FindInventory]
	@SupplierID int,
  @Brandid int,
  @ProductTypeid int,
	@UserID uniqueidentifier
  
as
begin

SET FMTONLY OFF;
declare @Hyphen char(2)
declare @sql varchar(8000)
declare @sqlWhere varchar(8000)
Declare @None char(4)
set @None = 'None'

set @Hyphen = '-'

set @sqlWhere = ' dbo.ProductsLU.UserID = ''' + cast(@UserID as varchar(100)) + ''''

if @SupplierID > 0
begin
  set @sqlWhere = @sqlWhere + ' and dbo.ProductsLU.SupplierID = ' + cast(@SupplierID as char(10))
end
if @BrandID > 0
begin
  set @sqlWhere = @sqlWhere + ' and dbo.BrandsLU.BrandID = ' + cast(@BrandID as char(10))
end 
if @ProductTypeID > 0
begin
  set @sqlWhere = @sqlWhere + ' and dbo.ProductsLU.ProductTypeID = ' + cast(@ProductTypeID as char(10))	
end

set @sqlWhere = 'WHERE ' + @sqlWhere 

set @sql = 'SELECT dbo.ProductsLU.LUProdSellUseID, dbo.ProductsLU.UserID, dbo.ProductsLU.SupplierID, dbo.ProductsLU.ProductTypeID, dbo.ProductsLU.ProductName, 
                      dbo.ProductTypesLU.ProductType, dbo.BrandsLU.BrandName, dbo.BrandsLU.BrandID, dbo.Suppliers.SupplierName, ISNULL(dbo.ProductDetails.ItemNumb, ''' + @None + ''') 
                      AS ItemNumb, ISNULL(dbo.ProductDetails.ItemNumb,''' + @None + ''') ''' + @hyphen + '''  dbo.ProductsLU.ProductName AS ItemProduct, ISNULL(dbo.ProductDetails.OuncesInStock, 0) 
                      AS OuncesInStock, ISNULL(dbo.ProductDetails.TotalQty, 0) AS TotalQty, dbo.ProductDetails.Discontinued
					FROM dbo.ProductsLU INNER JOIN
                      dbo.ProductTypesLU ON dbo.ProductsLU.ProductTypeID = dbo.ProductTypesLU.ProductTypeID INNER JOIN
                      dbo.BrandsLU ON dbo.ProductTypesLU.BrandID = dbo.BrandsLU.BrandID INNER JOIN
                      dbo.ProductDetails ON dbo.ProductsLU.LUProdSellUseID = dbo.ProductDetails.LUProdSellUseID INNER JOIN
                      dbo.Suppliers ON dbo.ProductsLU.SupplierID = dbo.Suppliers.SupplierID '





set @sql = @sql + @sqlWhere + 
    ' ORDER BY dbo.Suppliers.SupplierName, dbo.BrandsLU.BrandName, dbo.ProductTypesLU.ProductType, dbo.ProductsLU.ProductName '

exec( @sql )
  
end

Open in new window

0
 
LVL 15

Accepted Solution

by:
derekkromm earned 500 total points
ID: 34985461
at line 38, you need to change

''' + @hyphen + '''

to

+ ''' + @hyphen + ''' +
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 34985499
In general you'll find it easier to understand and maintain if you write these types of procedure like this.....
(and alias your tables...)

ie where you can see the shape of the sql statement as its being developed and the clauses are in the correct order....

always include code to "Print" the SQL statement so that you can easily see what you are attempting to execute
 (at least during development)

you basic error was not enclosing the userid in quotes  
and you had missed off the USERID column name in the first where clause element....


in general however it is a bad idea to write dynamic sql , for what appears to be a fairly simple sql statement...
unless you have a truly horrendous amount of optional conditions (and they could affect the access path significantly,
ie your fairly certain that different indexes should be used in certain circumstances)

are you sure that  writing it like this wouldn't perform efficiently?


ALTER PROCEDURE  [dbo].[stp_FindInventory]
      @SupplierID int,
  @Brandid int,
  @ProductTypeid int,
      @UserID uniqueidentifier
 
as
begin

SET FMTONLY OFF;

SELECT PLU.LUProdSellUseID, PLU.UserID, PLU.SupplierID, PLU.ProductTypeID, PLU.ProductName
           , PTLU.ProductType, BLU.BrandName, BLU.BrandID, S.SupplierName
             , coalesce(PD.ItemNumb, 'None') AS ItemNumb
              , coalesce(PD.ItemNumb,'None') +'- '+  PLU.ProductName AS ItemProduct
             , coalesce(PD.OuncesInStock, 0) AS OuncesInStock
              , coalesce(PD.TotalQty, 0) AS TotalQty, PD.Discontinued
              FROM dbo.ProductsLU as PLU  
            INNER JOIN dbo.ProductTypesLU as PTLU
                   ON PLU.ProductTypeID = PTLU.ProductTypeID
                   INNER JOIN dbo.BrandsLU as BLU
                ON PTLU.BrandID = BLU.BrandID
              INNER JOIN dbo.ProductDetails as PD
                 ON PLU.LUProdSellUseID = PD.LUProdSellUseID
              INNER JOIN dbo.Suppliers as S
                ON PLU.SupplierID = S.SupplierID
         Where PTLU.Userid = @UserID  
           and ( ( @SupplierID > 0
                  and PLU.SupplierID =@SupplierID )
                 or @supplierid < 1)
               
           and ( (  @BrandID > 0
                    and BLU.BrandID = @BrandID )
                 or @brandid < 1 )
               
          and (   ( @ProductTypeID > 0
                      and PLU.ProductTypeID = @ProductTypeID )      
                   or @productid < 1)
             
         ORDER BY S.SupplierName, BLU.BrandName, PTLU.ProductType, PLU.ProductName

RETURN  
end

use achtest
go
ALTER PROCEDURE  [dbo].[stp_FindInventory]
	@SupplierID int,
  @Brandid int,
  @ProductTypeid int,
	@UserID uniqueidentifier
  
as
begin

SET FMTONLY OFF;

declare @sql varchar(8000)

set @sql = 'SELECT PLU.LUProdSellUseID, PLU.UserID, PLU.SupplierID, PLU.ProductTypeID, PLU.ProductName' 
         + '     , PTLU.ProductType, BLU.BrandName, BLU.BrandID, S.SupplierName'
         + '     , coalesce(PD.ItemNumb, ''None'') AS ItemNumb'
         + '     , coalesce(PD.ItemNumb,''None'') +''- ''+  PLU.ProductName AS ItemProduct'
         + '     , coalesce(PD.OuncesInStock, 0) AS OuncesInStock'
         + '     , coalesce(PD.TotalQty, 0) AS TotalQty, PD.Discontinued'
		 + '  FROM dbo.ProductsLU as PLU'  
	     + ' INNER JOIN dbo.ProductTypesLU as PTLU' 
		 + '	ON PLU.ProductTypeID = PTLU.ProductTypeID' 
		+ '	 INNER JOIN dbo.BrandsLU as BLU' 
		+ '	    ON PTLU.BrandID = BLU.BrandID'
		+ '  INNER JOIN dbo.ProductDetails as PD'
		+ '     ON PLU.LUProdSellUseID = PD.LUProdSellUseID'
		+ '  INNER JOIN dbo.Suppliers as S'
		+ '     ON PLU.SupplierID = S.SupplierID '
        + ' Where PTLU.Userid = ''' + cast(@UserID as varchar(100)) + '''' 
        + case when @SupplierID > 0
               then ' and PLU.SupplierID = ' + cast(@SupplierID as char(10))
               else ''
               end
        + case when @BrandID > 0
               then ' and BLU.BrandID = ' + cast(@BrandID as char(10))
               else ''
               end 
        + case when @ProductTypeID > 0
               then ' and PLU.ProductTypeID = ' + cast(@ProductTypeID as char(10))	
               else ''
               end 
        + ' ORDER BY S.SupplierName, BLU.BrandName, PTLU.ProductType, PLU.ProductName '

Print @sql  -- for debugging comment out when going live

exec( @sql )

RETURN  
end

Open in new window

0
 

Author Comment

by:Sheritlw
ID: 34985518

Thank you so much.  It is working great.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
T-SQL to Update Table Dynamically 2 62
Are triggers slow? 7 25
Database maintenance 36 108
denied execute as 13 31
This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

733 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