Solved

Can't Find Error in stored procedure

Posted on 2011-02-25
7
191 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
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.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

832 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