?
Solved

Can't Find Error in stored procedure

Posted on 2011-02-25
7
Medium Priority
?
199 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

Question has a verified solution.

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

Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
Integration Management Part 2
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

864 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