2

UPDATED.

I made modifications to the Store Procedure:

ALTER PROCEDURE [dbo].[Get_Labor_Rates_By_CompanyID]
--Get_Labor_Rates_By_CompanyID '5116' ,'FOREMAN-REGULAR TIME (8hr Day Monday-Friday)* (HR)'

@CompanyID bigint,
@Labor_Type nvarchar(200)

AS
BEGIN

Select COLUMN_NAME from 
[Emerald_Data].INFORMATION_SCHEMA.COLUMNS
Where TABLE_NAME= N'tbl_Client_List_Pricing' and 
COLUMN_NAME=@Labor_Type

Select * from tbl_Client_List_Pricing
where [CompanyNum]= @CompanyID

END

After Running I have the following two tables from the select statements: enter image description here

I need to combine the two statements so the top COLUMN_NAME result filters the second Select to only show that Column and it's data for the Client Alston Construction. The Top COLUMN_NAME also exists in the bottom select as a COLUMN_NAME

The End Result Should look like:

Client                |   FOREMAN-REGULAR TIME(8hr Day Monday-Friday)* (HR)
----------------------------------------------------------------------------
Alston Construction   |   45
4
  • 1
    Side note: you should not use the sp_ prefix for your stored procedures. Microsoft has reserved that prefix for its own use (see Naming Stored Procedures), and you do run the risk of a name clash sometime in the future. It's also bad for your stored procedure performance. It's best to just simply avoid sp_ and use something else as a prefix - or no prefix at all! Commented Mar 1, 2018 at 5:59
  • Given code does not have invalid syntax. It is supposed to be somewhere near dynamic sql. Commented Mar 1, 2018 at 6:07
  • Something mysterious is going on here. Are you asking about magic formulas that are supposed to perform computations based on "column name"? Where would you take that column from if select * does not return it? And where did invalid syntax exception go? Commented Mar 1, 2018 at 8:05
  • By editing the content of your question, you've now made my answer look completely irrelevant. It's better just to ask a new question if you have a new question. I have updated my entire Answer now to match the usage of your new parameter names so that it's not confusing to whoever visits this page in the future. Commented Mar 1, 2018 at 16:08

1 Answer 1

2

Answer to the current version of your question (Please do not update it again):

You don't need to SELECT the column name from the table by itself first and then try to join it to the query you want. Your SELECT * FROM tbl_Client_List_Pricing already contains the column 'FOREMAN-REGULAR TIME (8hr Day Monday-Friday)* (HR)', so you can use dynamic sql to do this:

For example:

DECLARE @sql NVARCHAR(MAX)

SET @sql = N'SELECT [Client], [' + @Labor_Type + ']' + '
FROM tbl_Client_List_Pricing
WHERE [CompanyNum] = @CompanyID'

--PRINT @sql

EXEC (@sql)

To provide you with the results:

+---------------------+---------------------------------------------------+
|       Client        | FOREMAN-REGULAR TIME(8hr Day Monday-Friday)* (HR) |
+---------------------+---------------------------------------------------+
| Alston Construction |                      45                           |
+---------------------+---------------------------------------------------+

Your original question:

You asked why you were getting the following error, when trying to execute a stored procedure like this:

sp_Get_Labor_Rates_By_CompanyID '5204' 'STRAW WATTLE > 10000 LF-INSTALLED-Photo (LF)'

Incorrect syntax near 'STRAW WATTLE > 10000 LF-INSTALLED-Photo (LF)'


My Answer:

When you execute a stored procedure with more than one parameter, use a comma between your parameters.


Check out Execute a stored procedure or function

As you'll see there, you could either supply the parameter names:

[ [ @parameter = ] { value   
                           | @variable [ OUTPUT ]   
                           | [ DEFAULT ]   
                           }  
        ] 

Execution:

sp_Get_Labor_Rates_By_CompanyID @CompanyID = '5204', @Material_Type = 'STRAW WATTLE > 10000 LF-INSTALLED-Photo (LF)'`

OR

Supply them in the order given in the CREATE PROCEDURE statement (at the time of execution):

[ ,...n ] 

sp_Get_Labor_Rates_By_CompanyID '5204', 'STRAW WATTLE > 10000 LF-INSTALLED-Photo (LF)'

If you supply parameters in the form @parameter = value, you can supply them in any order. You can also omit parameters for which defaults have been supplied. If you supply one parameter in the form @parameter = value, you must supply all subsequent parameters this way. If you do not supply parameters in the form @parameter = value, you must supply them in the order given in the CREATE PROCEDURE statement.

Sign up to request clarification or add additional context in comments.

7 Comments

I made modifications to the Post. Please take a look again. Thank you!
That will not work as FOREMAN-REGULAR... is a variable based on the COLUMN_NAME returned. It's not the only COLUMN that can be returned from @Labor_Type Select Client, @Labor_Type from tbl_Client_List_Pricing where [CompanyNum]= @CompanyID Returns: Client | (No column name) -------------------------------------------------------------------------------------------- Alston Construction | FOREMAN-REGULAR TIME... @LaborType needs to be used to get the correct COLUMN_NAME and return it's Value based on the @CompanyID
Ok, I understand what you are asking now. I have updated my answer. You want to use ['+@Labor_Type+']
Invalid column name for that value. I am using MSSQL. I'm not sure if that might be for MySQL or not. `
Ok Thanks. I can't even Alter the SP because it's showing red as an invalid column name: Select Client, ['+@Labor_Type+'] from tbl_Client_List_Pricing where [CompanyNum]= @CompanyID Msg 207, Level 16, State 1, Procedure sp_Get_Labor_Rates_By_CompanyID, Line 13 [Batch Start Line 18] Invalid column name ''+@Labor_Type+''.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.