Solved

Can't Find Error in stored procedure

Posted on 2011-02-25
7
189 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
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.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

10 Experts available now in Live!

Get 1:1 Help Now