?
Solved

Can't Find Error in stored procedure

Posted on 2011-02-25
7
Medium Priority
?
196 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
[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
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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

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 2000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

770 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