Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
siekwel
Starting Member
1 Post |
Posted - 2003-01-14 : 06:49:28
|
| Hi,I have this stored procedure for searching data.Input are four parameters, on which should be search if not empty.I receive errormessages : x not a column name, with x the content of an input parameter?Does somebody recognize the failure?thnx in advance...BALTER PROCEDURE st_pr_search ( @Specified_material varchar(50), @UN_number varchar(20), @ProductName varchar(40), @SupplierName varchar(50) /*@parameter2 datatype OUTPUT*/ )ASDeclare @SQL VarChar(1000) Select @SQL = 'SELECT Material.Specified_material, Products.ProductName, Suppliers.SupplierName ' Select @SQL = @SQL + 'FROM Material 'Select @SQL = @SQL + 'INNER JOIN Suppliers ON Material.SupplierID=Suppliers.SupplierID 'Select @SQL = @SQL + 'INNER JOIN Products ON Material.ProductID=Products.ProductID 'Select @SQL = @SQL + 'WHERE 1=1 'IF NOT(@ProductName='' or @ProductName IS NULL) BEGIN Select @SQL = @SQL + 'AND Products.ProductName= ' + @ProductName ENDIF NOT(@Specified_material='' or @Specified_material IS NULL) BEGIN Select @SQL = @SQL + ' AND Material.Specified_material= ' + @Specified_material END IF NOT(@UN_number='' or @UN_number IS NULL) BEGIN Select @SQL = @SQL + ' AND Products.UN_number= ' + @UN_number END IF NOT(@SupplierName='' or @SupplierName IS NULL) BEGIN Select @SQL = @SQL + ' AND Suppliers.SupplierName=' + @SupplierName END Exec ( @SQL) /* SET NOCOUNT ON */ RETURN |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2003-01-14 : 06:51:31
|
| For string variablesSelect @SQL = @SQL + ' AND Suppliers.SupplierName=''' + @SupplierName + ''''If you print out the string you are executing you will see the problem.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
afterburn
Starting Member
28 Posts |
Posted - 2003-01-17 : 11:22:25
|
| First off the select @SQL = is a record set. If you query this in ADO you will get a recordset that has multiple recordsets in it.If you do not need them I would suggest that you useSET @SQL = ''other than that you have to double quote the data. If you need to quote data for string literals then SQL requires that you place 2 like soSelect * from tbl where field = 'she''s' SQL will look at it like a string literal.Looking at you sp I find that you will have about 4 errors first off you sp does not allow null values passed to it. which means that you do not need to check the value if not (@ProductName not is null and @ProductName <> '')this statement could be like soif @ProductName is not null begin select 0 selectEdited by - afterburn on 01/17/2003 11:25:50 |
 |
|
|
graz
Chief SQLTeam Crack Dealer
4149 Posts |
Posted - 2003-01-17 : 21:12:55
|
quote: First off the select @SQL = is a record set. If you query this in ADO you will get a recordset that has multiple recordsets in it.If you do not need them I would suggest that you useSET @SQL = ''
afterburn,I've used select @a = 'hello'or some such many times and it's never returned a recordset for that.===============================================Creating tomorrow's legacy systems today.One crisis at a time. |
 |
|
|
|
|
|