Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7713459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:52:51+00:00 2026-06-01T01:52:51+00:00

im not the best when it comes to SQL still learning the ropes. I

  • 0

im not the best when it comes to SQL still learning the ropes.
I have a Stored procedure in my SQL server manager 2008.

USE [ShaftData]
GO
/****** Object:  StoredProcedure [dbo].[GetSalesBuyers]    Script Date: 03/23/2012  08:13:17 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[GetSalesBuyers] 
@Cdisc varchar(255),
@bcs varchar(255), 
@From date, 
@Too date
AS
SELECT i.Acct,
   i.Name, 
   i.Document, 
   i.Part, 
   i.Qty, 
   i.Unit, 
   dbo.NEWPareto.Pareto, 
   i.pg,
   dbo.MyPgTable.PgName,
   i.[DateTime],
   i.BinSeqNo,
   i.cdisc,
   i.bcs

FROM   
OPENQUERY(SACBAUTO, 'SELECT dbo.iHeads.acct,
                            dbo.iHeads.name,
                            dbo.iLines.Document,
                            dbo.iLines.Part,
                            dbo.iLines.Pg,
                            dbo.iLines.Qty,
                            dbo.iLines.unit,
                            dbo.iHeads.[DateTime], 
                            dbo.iLines.BinSeqNo, 
                            dbo.Customer.cdisc,
                            dbo.Customer.Bcs
                     FROM Autopart.dbo.iheads INNER JOIN   Autopart.dbo.iLines ON 
                     Autopart.dbo.Iheads.document = autopart.dbo.iLines.document
                     INNER JOIN Autopart.dbo.Customer ON Autopart.dbo.iheads.acct 
                     = Autopart.dbo.customer.keycode
                     GROUP By dbo.iHeads.acct,
                            dbo.iHeads.name,
                            dbo.iLines.Document,
                            dbo.iLines.Part,
                            dbo.iLines.Pg,
                            dbo.iLines.Qty,
                            dbo.iLines.unit,
                            dbo.iHeads.[DateTime],
                            dbo.iLines.BinSeqNo,
                            dbo.Customer.cdisc,
                            dbo.Customer.bcs
                      ') i
left JOIN
dbo.NEWPareto
ON 
i.Part collate SQL_Latin1_General_CP1_CI_AS = dbo.NEWPareto.Part 
left JOIN
dbo.MyPgTable 
ON  
 i.pg collate SQL_Latin1_General_CP1_CI_AS = dbo.MyPgTable.[pGroup]

WHERE
 (i.[DateTime] BETWEEN @From AND @Too) 
 AND i.cdisc = @Cdisc 
 AND i.bcs != @bcs
 AND i.pg != '60'
 AND i.pg != '61'
 AND i.pg != '62'

 GROUP BY i.Acct,
   i.Name, 
   i.Document, 
   i.Part, 
   i.Qty, 
   i.Unit, 
   dbo.NEWPareto.Pareto, 
   i.pg,
   dbo.MyPgTable.PgName, 
   i.[DateTime],
   i.BinSeqNo,
   i.cdisc,
   i.bcs

What I need is a condition in the Where clause. particular this row “AND i.bcs != @bcs”.

What happens is IF i pass an Empty string to my param i want this row to exist and run in the where clause.

Else IF i dont pass anything (null), i need the row to not exist(not run).

I have played around but all im getting is red lines everywhere when i attempt to create.

Does anyone have an idea? am i close in my methodology? can it be done? or is there a easy mode way that im over looking.
Many thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T01:52:53+00:00Added an answer on June 1, 2026 at 1:52 am

    Optional parameters are constructed like this:

    WHERE (@bcs is null OR i.bcs != @bcs)
    

    If you send null every row is selected; if you send anything else, only non-matching rows are selected. You shouldn’t worry about performance because Sql Server is very good with constant expressions, and if it evaluates to one it will be suppressed.

    USE [ShaftData]
    GO
    /****** Object:  StoredProcedure [dbo].[GetSalesBuyers]    Script Date: 03/23/2012  08:13:17 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    ALTER PROCEDURE [dbo].[GetSalesBuyers] 
    @Cdisc varchar(255),
    @bcs varchar(255), 
    @From date, 
    @Too date
    AS
    SELECT i.Acct,
       i.Name, 
       i.Document, 
       i.Part, 
       i.Qty, 
       i.Unit, 
       dbo.NEWPareto.Pareto, 
       i.pg,
       dbo.MyPgTable.PgName,
       i.[DateTime],
       i.BinSeqNo,
       i.cdisc,
       i.bcs
    
    FROM   
    OPENQUERY(SACBAUTO, 'SELECT dbo.iHeads.acct,
                                dbo.iHeads.name,
                                dbo.iLines.Document,
                                dbo.iLines.Part,
                                dbo.iLines.Pg,
                                dbo.iLines.Qty,
                                dbo.iLines.unit,
                                dbo.iHeads.[DateTime], 
                                dbo.iLines.BinSeqNo, 
                                dbo.Customer.cdisc,
                                dbo.Customer.Bcs
                         FROM Autopart.dbo.iheads INNER JOIN   Autopart.dbo.iLines ON 
                         Autopart.dbo.Iheads.document = autopart.dbo.iLines.document
                         INNER JOIN Autopart.dbo.Customer ON Autopart.dbo.iheads.acct 
                         = Autopart.dbo.customer.keycode
                         GROUP By dbo.iHeads.acct,
                                dbo.iHeads.name,
                                dbo.iLines.Document,
                                dbo.iLines.Part,
                                dbo.iLines.Pg,
                                dbo.iLines.Qty,
                                dbo.iLines.unit,
                                dbo.iHeads.[DateTime],
                                dbo.iLines.BinSeqNo,
                                dbo.Customer.cdisc,
                                dbo.Customer.bcs
                          ') i
    left JOIN
    dbo.NEWPareto
    ON 
    i.Part collate SQL_Latin1_General_CP1_CI_AS = dbo.NEWPareto.Part 
    left JOIN
    dbo.MyPgTable 
    ON  
     i.pg collate SQL_Latin1_General_CP1_CI_AS = dbo.MyPgTable.[pGroup]
    
    WHERE
     (i.[DateTime] BETWEEN @From AND @Too) 
     AND i.cdisc = @Cdisc 
     AND (@bcs is null OR i.bcs != @bcs)
     AND i.pg != '60'
     AND i.pg != '61'
     AND i.pg != '62'
    
     GROUP BY i.Acct,
       i.Name, 
       i.Document, 
       i.Part, 
       i.Qty, 
       i.Unit, 
       dbo.NEWPareto.Pareto, 
       i.pg,
       dbo.MyPgTable.PgName, 
       i.[DateTime],
       i.BinSeqNo,
       i.cdisc,
       i.bcs
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have updated our servers with Windows 2008 Server, but we are still using
I am still a noobie when it comes to the SQL server. I know
We have a legacy database which is a sql server db (2005, and 2008).
I find this comes up a lot, and I'm not sure the best way
This question is not about 'best' barcode library recommendation, we use various products on
I have heard that it's best not to actually have any html in your
in the sql server database I have a field that is type numeric. Precision
I know that a SQL Server full text index can not index more than
In my server application I want to use DB (SQL Server) but I am
I have a centrally hosted database (MS SQL Server) and distributed clients save data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.