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

  • SEARCH
  • Home
  • 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 7717865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:00:48+00:00 2026-06-01T03:00:48+00:00

CREATE FUNCTION [dbo].[MSG_FilterAutoship] ( @ItemID VARCHAR(50) = NULL ) RETURNS @Autoship TABLE ( DistID

  • 0
CREATE FUNCTION [dbo].[MSG_FilterAutoship]
(
    @ItemID VARCHAR(50) = NULL
)
RETURNS 
@Autoship TABLE 
(
    DistID INT,
    BusCtrID INT
)
AS
BEGIN
    INSERT INTO @Autoship (DistID, BusCtrID)
        SELECT [as].Distid, 1 as busctrid
        FROM Autoship [as]
        INNER JOIN AutoshipDetail ad ON [as].DistID = ad.DistID
        INNER JOIN AS_DistributorCreditCard acc ON [as].DistID = acc.DistID
        WHERE [ad].InventoryID IN (@ItemID)
    RETURN 
END

What I need to have happen is if @ItemID is passed in a null value, then WHERE [ad].InventoryID IN (@ItemID) would basically not filter the results at all. Is this possible?

So basically if I pass in 120, 520 it would filters the results so only those two items are shown. If I pass in NULL then it would show all items.

Split function:

ALTER FUNCTION [dbo].[Split]
(
    @Delimiter CHAR,
    @Text TEXT
)
RETURNS @Result TABLE (RowID SMALLINT IDENTITY(1, 1) PRIMARY KEY, Data VARCHAR(MAX))
AS

BEGIN
    DECLARE @NextPos INT,
        @LastPos INT

    SELECT  @NextPos = CHARINDEX(@Delimiter, @Text, 1),
        @LastPos = 0

    WHILE @NextPos > 0
        BEGIN
            INSERT  @Result
                (
                    Data
                )
            SELECT  SUBSTRING(@Text, @LastPos + 1, @NextPos - @LastPos - 1)

            SELECT  @LastPos = @NextPos,
                @NextPos = CHARINDEX(@Delimiter, @Text, @NextPos + 1)
        END

    IF @NextPos <= @LastPos
        INSERT  @Result
            (
                Data
            )
        SELECT  SUBSTRING(@Text, @LastPos + 1, DATALENGTH(@Text) - @LastPos)

    RETURN
END

Here is the updated query that uses the split function:

AS
BEGIN
    INSERT INTO @Autoship (DistID, BusCtrID)
        SELECT [as].Distid, 1 as busctrid
        FROM Autoship [as]
        INNER JOIN AutoshipDetail ad ON [as].DistID = ad.DistID
        INNER JOIN AS_DistributorCreditCard acc ON [as].DistID = acc.DistID
        WHERE [ad].InventoryID IN (SELECT Data from dbo.Split(',', @ItemID)) or @ItemID IS NULL 
    RETURN 
END
  • 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-01T03:00:50+00:00Added an answer on June 1, 2026 at 3:00 am

    Sure. WHERE InventoryID = @ItemID OR @ItemID IS NULL. Note that TSQL will not automatically expand “1, 2, 3” into “IN (1, 2, 3)”, rather it will compare InventoryID to “1, 2, 3” and presumably come up with no matches.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

CREATE FUNCTION dbo.GetCustomers ( @ZIPCode VARCHAR(255) ) RETURNS TABLE AS BEGIN SELECT * dbo.GetCustomers
I have simplified my function to the following: create function [dbo].[UserSuperTeams](@ProjectId int) returns table
Consider the following tsql... create function dbo.wtfunc(@s varchar(50)) returns varchar(10) begin return left(@s, 2);
I have the following function: CREATE FUNCTION fGetTransactionStatusLog ( @TransactionID int ) RETURNS varchar(8000)
here my code- create function dbo.emptable() returns Table as return (select id, name, salary
So I have this working properly: CREATE FUNCTION dbo.GetLiveStream(@UserName NVARCHAR(MAX)) RETURNS TABLE AS RETURN
I have this script: CREATE FUNCTION dbo.CheckIfSFExists(@param1 INT, @param2 BIT = 1 ) RETURNS
I've used the following split function: CREATE FUNCTION dbo.Splitfn(@String varchar(8000), @Delimiter char(1)) returns @temptable
I have a function that returns groups(clusters) from a table.. create function dbo.ftAllClusters() returns
why error? CREATE FUNCTION [dbo].[seth] (@IdUrl BIGINT) RETURNS TABLE AS RETURN(SELECT * FROM dbo.NetGraph

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.