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 8287361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:00:51+00:00 2026-06-08T12:00:51+00:00

I am a beginner with SQL and especially with TSQL. I need to write

  • 0

I am a beginner with SQL and especially with TSQL. I need to write a SP for SQL Server 2008 that will read all records that match some criteria and then read in different result sets their associated category, ingredients, units and so on. For reading one element my SP is:

-- Select the recipe 
SELECT Recipe.*
FROM Recipe
WHERE Recipe.RecipeId = @RecipeId

-- Select the categories themselves
SELECT Category.*
FROM Category
JOIN RecipeCategory ON RecipeCategory.CategoryId = Category.CategoryId
WHERE RecipeCategory.RecipeId = @RecipeId

-- Select the ingredient information for the recipe
SELECT RecipeIngredient.*
FROM RecipeIngredient
JOIN Recipe ON Recipe.RecipeId = RecipeIngredient.RecipeId
WHERE Recipe.RecipeId = @RecipeId

-- Select the ingredients themselves
SELECT Ingredient.* 
FROM Ingredient 
JOIN RecipeIngredient ON RecipeIngredient.IngredientId = Ingredient.IngredientId
JOIN Recipe ON Recipe.RecipeId = RecipeIngredient.RecipeId
WHERE Recipe.RecipeId = @RecipeId  

-- Select the units that are associated with the ingredients    
SELECT Unit.*
FROM Unit
JOIN Ingredient ON Ingredient.UnitId = Unit.UnitId
JOIN RecipeIngredient ON RecipeIngredient.IngredientId = Ingredient.IngredientId
WHERE RecipeIngredient.RecipeId = @RecipeId

How can I transform it to read all recipes that have Name like '%..%'

Since the table has millions of recipes I would like to do it as efficient as possible.

  • 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-08T12:00:53+00:00Added an answer on June 8, 2026 at 12:00 pm

    For selecting recipes by Name (with wildcards), you might alter your proc to do something like this:

    -- Get a list of name-matched RecipeIDs
    DECLARE @RecipeIDs TABLE (
        RecipeID int not null primary key
    )
    INSERT INTO @RecipeIDs (RecipeID)
    SELECT Recipe.RecipeID
    FROM Recipe
    -- Change the parameter of the proc from @RecipeId to @Name
    WHERE Recipe.Name like '%' + @Name + '%'
    
    -- Select the recipes 
    SELECT Recipe.*
    FROM Recipe
    WHERE Recipe.RecipeId in (select RecipeID from @RecipeIDs)
    
    -- Select the categories themselves
    SELECT Category.*
    FROM Category
        JOIN RecipeCategory ON RecipeCategory.CategoryId = Category.CategoryId
    WHERE RecipeCategory.RecipeId in (select RecipeID from @RecipeIDs)
    
    -- Select the ingredient information for the recipes
    SELECT RecipeIngredient.*
    FROM RecipeIngredient
        JOIN Recipe ON Recipe.RecipeId = RecipeIngredient.RecipeId
    WHERE Recipe.RecipeId in (select RecipeID from @RecipeIDs)
    
    -- Select the ingredients themselves
    SELECT Ingredient.* 
    FROM Ingredient 
        JOIN RecipeIngredient ON RecipeIngredient.IngredientId = Ingredient.IngredientId
        JOIN Recipe ON Recipe.RecipeId = RecipeIngredient.RecipeId
    WHERE Recipe.RecipeId in (select RecipeID from @RecipeIDs)  
    
    -- Select the units that are associated with the ingredients    
    SELECT Unit.*
    FROM Unit
        JOIN Ingredient ON Ingredient.UnitId = Unit.UnitId
        JOIN RecipeIngredient ON RecipeIngredient.IngredientId = Ingredient.IngredientId
    WHERE RecipeIngredient.RecipeId in (select RecipeID from @RecipeIDs)
    

    I’m first getting all of the recipe IDs that match a new @Name parameter, then getting your result sets using IN instead of =.

    As far as performance, be sure you’re getting the correct results first before trying to optimize for speed. However, if you have a performance problem, there are some other ways to write the query. For example, if the list of matched IDs gets huge, you might rather use temp table instead of a table variable to keep the list, or just in-line the name-matching part into every select individually. Maybe a join on the RecipeIDs would be faster than the IN. Of course, the SQL engine may do very much the same thing in all of those cases (SQL is essentially declarative, after all). Indexing of the tables could come into play as well. Please let us know how this works out for you.

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

Sidebar

Related Questions

SQL server 2008 sp2 I am a beginner and finding that XQuery syntax especially
Please help this beginner here... I have a SQL Server 2008 R2 running on
I am a beginner in sql server.I have read about buffer cache in sql
I've got a timestamp column in SQL Server 2008. Now I need to query
SQL 2008 - Beginner to DBA I am confused between SQL Server Network Configuration
I am a beginner SQL user (not formally trained; OJT only) and need some
I'm a beginner in SQL Server 2008 and i have difficulty to find solution
I am a very beginner with sql and I need to write a SP
SQL Server Beginner question: I'm trying to introduce a computed column in SQL Server
I'm a beginner with C#, ASP.NET, SQL Server Express and programming in general. I'm

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.