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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:46:27+00:00 2026-06-09T07:46:27+00:00

Here is my sql query. CREATE FUNCTION UF_GetOrderProducts ( @OrderId int ) RETURNS VARCHAR(500)

  • 0

Here is my sql query.

CREATE FUNCTION UF_GetOrderProducts
(
    @OrderId int
)
RETURNS VARCHAR(500)
AS
BEGIN
    SELECT  Identity(int,1,1) ID, ProductId INTO #Temp  FROM OrderProduct WHERE OrderId = @OrderId

Declare @Id int,
        @Count int,
        @LoopCount int,
        @ProductList VARCHAR(500),
        @ProductListTemp VARCHAR(500)

SET @Count = (Select Count(*) From #Temp)

SET @LoopCount = 1
 SET @ProductList = ''
WHILE @LoopCount <= @Count
BEGIN



     SET @ProductListTemp =( SELECT Name FROM Product WHERE ProductId =(Select ProductId from #Temp Where ID = @LoopCount))
       SET @ProductList +=@ProductListTemp + '<br/>'
       Set @LoopCount=@LoopCount + 1         


END
DROP TABLE #Temp

RETURN @ProductList

END
GO

I have to loop in #Temp Table. Do you have any other suggestions?

  • 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-09T07:46:29+00:00Added an answer on June 9, 2026 at 7:46 am

    Instead of temp table you can use a table variable.

    declare @Temp TABLE (ID int identity, ProductId int)
    
    insert into @Temp(ProductId)
    select ProductId 
    from OrderProduct
    where OrderId = @OrderId
    

    But you could rewrite your function without a loop.

    Something like this should do what you want.

    create function IF_GetOrderProducts
    (
      @OrderId int
    )
    returns varchar(500)
    as
    begin
      return
        (
        select Name+'<br/>'
        from Product as P
          inner join OrderProduct as OP
            on P.ProductId = OP.ProductId
        where OP.OrderId = @OrderId
        for xml path(''), type
        ).value('.', 'varchar(500)')
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

SQL server 2008 Hello here is my query which returns the result SELECT *
Here is my SQL script CREATE TABLE tracks( track_id int NOT NULL AUTO_INCREMENT, account_id
Here is my sql: CREATE TABLE companyprinciple { userid INT, FOREIGN KEY (userid) REFERENCES
So I'm trying to create a function that generates a SQL query string based
Here is a SQL query which fail if multiple values are returned by first-
I'm having an issue extracting a substring in SQL query results. Here's the situation:
here is the sql codes SELECT roles.role_id, usertype_module.role_id, usertype_module.viewable, usertype_module.editable, usertype_module.createable, modules.modules_id, usertype_module.modules_id, modules.modules,
In SQL Server 2008 I would like to create a stored procedure to select
I'm trying to create a query on SQL server 2005 that will check if
Pulling my hair out, trying to figure out what's wrong here. Create table query:

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.