Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 19th, 2004, 01:30 PM
Registered User
 
Join Date: Mar 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Select statement

I have the following stored procedure in which gives me an error with the select statement (at the end). I am not sure how to correct it. Please let me know if you have any suggestions. Thanks

Stored Procedure
---------------------------------------------------------------------
CREATE PROCEDURE [dbo].[Usp_selTIMS_EXPT_FAD_BASIC]
    @ddlOverallExpertise varchar(1000),
    @ddlOverallExpertise_count int,
    @ddlcond1 varchar(10),
             @ddlSpecificExpertise varchar(1000),
             @ddlSpecificExpertise_count int,
    @ddlcond2 varchar(10),
             @ddlLanguage varchar(1000),
             @ddlLanguage_count int,
    @ddlLangCond varchar(10),
    @ddlFluency varchar(1000),
             @ddlFluency_count int,
             @ddlNationality varchar(1000),
             @ddlNationality_count int,
    @ddlCondNation varchar(1000),
    @ddlLastUsed varchar(1000),
             @ddlLastUsed_count int,
             @ddlStatus varchar(1000),
             @ddlStatus_count int

AS

set nocount on

declare @separator char(1)
declare @separator_position int
declare @OverallExpertise varchar(1000)
declare @SpecificExpertise varchar(1000)
declare @Language varchar(1000)
declare @strSelect varchar(5000)
declare @strWhere varchar(8000)
declare @strWhere1 varchar(8000)
declare @strWhere2 varchar(8000)
declare @strWhere3 varchar(8000)
declare @strWhere4 varchar(8000)
declare @strWhere5 varchar(8000)
declare @strWhere6 varchar(8000)
declare @blnFirstParameter int

set @separator = ','
set @ddlOverallExpertise = @ddlOverallExpertise + @separator
set @ddlSpecificExpertise = @ddlSpecificExpertise + @separator
set @ddlLanguage = @ddlLanguage + @separator
set @blnFirstParameter = 1
set @strWhere = ''
set @strWhere1 = ''
set @strWhere2 = ''
set @strWhere3 = ''
set @strWhere4 = ''
set @strWhere5 = ''
set @strWhere6 = ''

if @ddlOverallExpertise_count > 0
begin
  while patindex('%' + @separator + '%', @ddlOverallExpertise) <> 0
  begin
    select @separator_position = patindex('%' + @separator + '%', @ddlOverallExpertise)
    select @OverallExpertise = left(@ddlOverallExpertise, @separator_position - 1)

    -- processing array value

    if @ddlOverallExpertise_count = 1
    begin
      set @strWhere = @strWhere + ' TIMS_FAD_EXPT_BASIC.emplid IN '
      set @strWhere = @strWhere + ' ( SELECT TIMS_FAD_EXPT_XPTIZ.emplid from TIMS_FAD_EXPT_XPTIZ WHERE
                                                    TIMS_FAD_EXPT_XPTIZ.TIMS_ovr_expertise ' + @ddlcond1 + ' ' + @OverallExpertise + ')'
    end

    select @ddlOverallExpertise = stuff(@ddlOverallExpertise, 1, @separator_position, '')
  end
end



.
.
. (similar code like above .... assigns @strWhere1,@strWhere2 ...)
.
.
.


SELECT * FROM TIMS_FAD_EXPT_BASIC WHERE + @strWhere + @strWhere1 + @strWhere2 + @strWhere3 + @strWhere4 + @strWhere5 + @strWhere6 + ORDER BY TIMS_FAD_EXPT_BASIC.NAME

set nocount off
GO



 
Old March 30th, 2004, 03:16 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

You need to do something like:

declare @SQL varchar(8000)

set @SQL = 'select * from TIMS_FAD_EXPT_BASIC where ' + @strWhere + @strWhere1 + @strWHere2 + @strWhere3 + @strWhere4 + @strWhere5 + @strWhere6 + ' order by TIMS_FAD_EXPT_BASIC.NAME'

Also, you should note that if any of the where variables is null, it may cause problems. You will want to set:

set concat_null_yields_null off

So that when you concatenate a null value, it doesn't null the string.

Brian





Similar Threads
Thread Thread Starter Forum Replies Last Post
select statement ??? RinoDM SQL Server 2000 7 June 19th, 2008 08:40 AM
select Statement gregalb SQL Server 2000 3 January 15th, 2008 12:00 AM
select statement help... RinoDM SQL Server 2000 13 January 10th, 2008 08:34 PM
Select from another select statement to a repeater simsen ASP.NET 2.0 Professional 0 May 2nd, 2007 04:34 PM
Select Statement jmss66 Oracle 1 May 27th, 2004 02:31 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.