views:

75

answers:

4

I just creates this stored procedure in selecting palletnumber=datatype is int. SerialNumber is varchar. Then pallet numbers are 1 - 200..But if i enter 2 it shows 2 with other palletnumbers. I change @search to nchar and its ok but serialnumber is has error. Im new in stored procedure.

ALTER PROCEDURE [dbo].[sp_SearchFresh]
    -- Add the parameters for the stored procedure here
    @Search varchar(50)--so that i only have one search engine/input box
AS
SELECt  dbo.Monitor.SerialNumber, 
      WIP.dbo.WIPTEST.PartNumber,
      dbo.Monitor.PalletNumber
FROM  WIP.dbo.WIPTEST 
      INNER JOIN dbo.Monitor 
         ON WIP.dbo.WIPTEST.SerialNumber = dbo.Monitor.SerialNumber
WHERE WIP.dbo.WIPTEST.StatusDescription = 'FG-FRESH' 
    AND (dbo.Monitor.PalletNumber = @Search 
        OR dbo.Monitor.SerialNumber LIKE @Search + '%' )
END

it works..i will practice dynamic SQL. But how can i supply null values on my ui? this is my DAL code

public DataSet FGSearch_Grid(Ientity user) {

        SqlConnection conn = new SqlConnection(connStr);
        SqlCommand cmd = new SqlCommand("sp_SearchFresh", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        DataSet dSet = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        cmd.Parameters.Add("@Search", SqlDbType.VarChar, 50).Value = user.Search;

        try
        {
            conn.Open();

            da.Fill(dSet, "WIPDATA");

            conn.Close();
        }
        catch (SqlException)
        {
            throw;
        }
        return dSet;
    }
A: 

Make sure that the pallet numbers that you are getting doesn't have a serial number starting with 2?

Michael Rodrigues
+1  A: 

To have the @Search and palletnumber evaluate as int try

AND (dbo.Monitor.PalletNumber = cast(@Search as int)
DaveWilliamson
And what happens when @Search doesn't contain any numeric values? ;)
OMG Ponies
That is a valid concern. However, for the scope of his question I read that the main focus right now was using the same parameter against 2 different data types.
DaveWilliamson
+2  A: 

You're quickly finding that using a single parameter doesn't work well to serve multiple filter criteria that has different data types. Make things easier on yourself, and accept that for every separate filter criteria you want to provide, must have a separate stored procedure arguement parameter.

ALTER PROCEDURE [dbo].[sp_SearchFresh]    
  @Searchvarchar(50),
  @pallet_num INT

AS

  SELECT dbo.Monitor.SerialNumber,      
              WIP.dbo.WIPTEST.PartNumber,      
              dbo.Monitor.PalletNumber
    FROM  WIP.dbo.WIPTEST       
      JOIN dbo.Monitor ON WIP.dbo.WIPTEST.SerialNumber = dbo.Monitor.SerialNumber
  WHERE WIP.dbo.WIPTEST.StatusDescription = 'FG-FRESH'   
     AND (@pallet_num IS NULL OR dbo.Monitor.PalletNumber = @pallet_num)
     AND (@Search IS NULL OR dbo.Monitor.SerialNumber LIKE @Search+'%')

END
OMG Ponies
So i need to create different input box on my UI?
Crimsonland
@Crimsonland: Yes - you should add inputs to your UI. It'll also be more obvious to your users what the data is used for, and how to use the form.
OMG Ponies
Ok sir i will try to do this. how can i show the values using list view control using n-tier? Currently i am using Data grid and dataset to filter my search,i dont know yet about data readers.
Crimsonland
@OMG Ponies: Be careful with Catch All Queries. See my answer below. Greets, Flo
Florian Reischl
@Florian Reischl: Dynamic SQL is my preference for situations like these, but that's only worth pursuing when the OP accepts that these should be separate parameters. Also, some shops don't allow dynamic SQL as a rule, so I tread carefully when suggesting a dynamic SQL solution.
OMG Ponies
@OMG Ponies: Makes sense. Thanks for the feedback.
Florian Reischl
A: 

Be careful with "Catch All Queries" since they always cause a full table scan. If you don't know them, have a look at Gail Shawn's blog Catch All Queries.

The only way to ensure index utilization with optional arguments is dynamic SQL (ensure using sp_executesql to avoid SQL injection):

ALTER PROCEDURE [dbo].[sp_SearchFresh]    
  @search varchar(50),
  @pallet_num INT
AS

DECLARE @sql NVARCHAR(4000);

SET @sql = N'
  SELECT dbo.Monitor.SerialNumber,      
              WIP.dbo.WIPTEST.PartNumber,      
              dbo.Monitor.PalletNumber
    FROM  WIP.dbo.WIPTEST       
      JOIN dbo.Monitor ON WIP.dbo.WIPTEST.SerialNumber = dbo.Monitor.SerialNumber
  WHERE WIP.dbo.WIPTEST.StatusDescription = ''FG-FRESH'''

IF (@search IS NOT NULL)
BEGIN
   SET @sql = @sql + ' AND dbo.Monitor.SerialNumber LIKE @searchIN';
   SET @search = REPLACE(@search, '%', '%%') + '%';
END

IF (@pallet_num IS NOT NULL)
   SET @sql = @sql + ' AND dbo.Monitor.PalletNumber = @pallet_numIN';

EXECUTE sp_executesql 
            @sql
            ,N'@searchIN VARCHAR(50), @pallet_numIN INT'
            ,@searchIN = @search
            ,@pallet_numIN = @pallet_num;

Greets Flo

Florian Reischl
it works..i will practice dynamic SQL. But how can i supply null values on my ui? this is my DAL code
Crimsonland
param.Value = DBNull.Value;
Florian Reischl