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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:20:17+00:00 2026-06-07T01:20:17+00:00

Suppose you have a long, complicated query to pull needed results that takes some

  • 0

Suppose you have a long, complicated query to pull needed results that takes some parameters. Any will do, but for the sake of an example:

SELECT
    q.PROD_ID, q.NAME, q.STANDARD_PROD, q.DESCRIPTION, q.PART_NUMBER,
    q.COMMENTS, q.DESCRIPTION_URL,
    PROD_CATEGORY.DESCRIPTION AS CATEGORY_DESCRIPTION,
    PROD_TYPES.DESCRIPTION AS PROD_TYPE
FROM
    (SELECT
        PROD.PROD_ID,
        PROD.PROD_TYPE_ID,
        PROD.NAME,
        PROD.STANDARD_PROD,
        PROD.PROD_CATEGORY_ID,
        PROD.DESCRIPTION,
        PROD.PART_NUMBER,
        PROD.COMMENTS,
        PROD.DESCRIPTION_URL
     FROM
        (SELECT
            PROD_ID,
            PROD_TYPE_ID
         FROM
            XREF_PRODSYS
         WHERE
            (PROD_TYPE_ID = (SELECT
                                PROD_TYPE_ID
                             FROM
                                PROD_TYPES
                             WHERE
                                (NAME LIKE @prod_type_name)))) AS p
     LEFT OUTER JOIN PROD ON p.PROD_ID = PROD.PROD_ID
     WHERE
        (PROD.NAME LIKE @prod_name)
        AND (PROD.HIDDEN = 0)) AS q
LEFT OUTER JOIN PROD_CATEGORY ON q.PROD_CATEGORY_ID = PROD_CATEGORY.PROD_CATEGORY_ID
LEFT OUTER JOIN PROD_TYPES ON q.PROD_TYPE_ID = PROD_TYPES.PROD_TYPE_ID

This particular query takes two parameters, presumably passed to the .NET web app through GET/POST.

Is there a cleaner way to store such a long query, rather than putting it in the C# source of the webapp page? I know the below “quick and dirty” approach works fine, but it does
make the code expand a lot and become a bit unmanageable. For example:

    //inside Page_Load...
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;
    cmd.Parameters.Add("@prod_type_name", SqlDbType.VarChar).Value = _type_name;
    cmd.Parameters.Add("@prod_name", SqlDbType.VarChar).Value = _prod_name;
    cmd.CommandText = @"
    SELECT     q.PROD_ID, q.NAME, q.STANDARD_PROD, q.DESCRIPTION, q.PART_NUMBER, q.COMMENTS, q.DESCRIPTION_URL, 
              PROD_CATEGORY.DESCRIPTION AS CATEGORY_DESCRIPTION, PROD_TYPES.DESCRIPTION AS PROD_TYPE
    FROM         (SELECT     PROD.PROD_ID, PROD.PROD_TYPE_ID, PROD.NAME, PROD.STANDARD_PROD, PROD.PROD_CATEGORY_ID, PROD.DESCRIPTION, 
                                      PROD.PART_NUMBER, PROD.COMMENTS, PROD.DESCRIPTION_URL
               FROM          (SELECT     PROD_ID, PROD_TYPE_ID
                                       FROM          XREF_PRODSYS
                                       WHERE      (PROD_TYPE_ID =
                                                                  (SELECT     PROD_TYPE_ID
                                                                    FROM          PROD_TYPES
                                                                    WHERE      (NAME LIKE @prod_type_name)))) AS p LEFT OUTER JOIN
                                      PROD ON p.PROD_ID = PROD.PROD_ID
               WHERE      (PROD.NAME LIKE @prod_name) AND (PROD.HIDDEN = 0)) AS q LEFT OUTER JOIN
              PROD_CATEGORY ON q.PROD_CATEGORY_ID = PROD_CATEGORY.PROD_CATEGORY_ID LEFT OUTER JOIN
              PROD_TYPES ON q.PROD_TYPE_ID = PROD_TYPES.PROD_TYPE_ID
              ";
              //... do stuff with cmd
  • 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-07T01:20:19+00:00Added an answer on June 7, 2026 at 1:20 am

    Why not just put this all into a stored procedure and call the stored procedure, passing in the parameters appropriately?

    How to create a stored procedure

    ...
    cmd.CommandText = "sprocname";
    cmd.CommandType = CommandType.StoredProcedure
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose you have a subsystem that does some kind of work. It could be
Suppose I have any long hash, like a 16 bytes MD5 or a 20
Suppose I have a relatively long module, but need an external module or method
Suppose that I have recorded a long macro with many commands and special characters.
Suppose I have a SQL Query that isn't really nice. The SQL Query has
Suppose I have an R data frame with columns that specify location (lat/long), height,
Suppose I have 2 servers. The first is a service that provides some computations,
Suppose I have a class like this: class Owner { private: long m_Id; QString
I have a python app which is supposed to be very long-lived, but sometimes
Suppose I have a long java String , I need to wrap all whitespaces

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.