Replacing All with Required columns.

  • Hi-- please advise on the below sceanrio

     

    SELECT * --- Question ) How can I replace "*" which is after select with column names, example the query is only looking for "Request_Status" in the WHERE clause so is it required that I still need to select Person_ID and additional columns. ( this code is part of an SP so wanted to avoid pasting to much code here and only pasted the part of Temp table which had the all in select)

    FROM

    (

    SELECT Row_Number() over(order by Person_LastName, Person_FirstName) ROWID,

    Person_ID,

    Person_FirstName,

    Person_LastName,

    Person_SSN,

    Person_Email,

    Employee_Status,

    Request_Status, -- This is what looks like it is looking for.

    Registration_ID,

    Registration_Opened,

    Registration_Closed

    FROM #tbPersonRegistration

    WHERE Request_Status = @Request_Status_ID -- which is one of the INT variable while creating the SP

    )as result

    WHERE ROWID between @StartRow and @EndRow

    END

    GO

    • This topic was modified 5 years, 2 months ago by  sizal0234.
  • You don't have to select all the columns - you can select one, two, three or whatever columns as long as they are part of the result set you are selecting from.

    Sue

  • Are you trying to build the dynamic T-SQL query to SELECT the dynamic columns and filter using WHERE clause using dynamic columns ? If yes, then you can use the dynamic parameterized SQL. You can refer the below URL for help.

    https://www.mssqltips.com/sqlservertip/1160/execute-dynamic-sql-commands-in-sql-server/

  • if you get Redgate sql prompt , there is an option to expand *

    I believe if you look at apexsql website you might also find a free tool that also does it - you can then just delete the column names from your code that you don't want

    MVDBA

  • Since it is a part of SP, there must be some logic and requirements of other columns in the remaining SP code.

    Get the requirements clear and eliminate the columns which are not at all required anywhere.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply