[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 138
  • Last Modified:

Formatting a SQL Statement in SQL Server?

Based on the SQL Code below, how can I construct this statement?

ALTER PROCEDURE [dbo].[spLoadEquipmentCalibrationGrid]
	@equipmentType_ID		INT=NULL,
	@calibration_ID			INT=NULL
AS
BEGIN
	SET NOCOUNT ON;
	
	DECLARE @strCalibration_IDs		VARCHAR(50)
	
	IF @calibration_ID = 4
		SET @strCalibration_IDs = '(2,3,4)'
	ELSE
		SET @strCalibration_IDs = '(' + @calibration_ID + ')'
		
	IF @equipmentType_ID = 0
		SELECT	equipment_ID AS 'Equipment ID'
		  FROM  HEADER_Equipment 
		 WHERE	A.equipmentType_ID > 0 AND calibration_ID IN @calibration_ID
	  ORDER BY	B.equipmentType

Open in new window

0
BlakeMcKenna
Asked:
BlakeMcKenna
  • 2
1 Solution
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Give us some Barney-level instructions on what you're trying to pull off here.
Not sure what you mean by 'Based on the SQL Code below, how can I construct this statement?'.
0
 
jogosCommented:
That is dynamic sql as first option construct the complete sql-string and then EXEC
http://msdn.microsoft.com/en-us/library/ms188332(v=sql.105).aspx
0
 
jogosCommented:
But better is to create a table variable and insert the values in that table depending on your test and select the wanted values in your select by using that table-variable
Declare @mytab table (val int);
if ... 
BEGIN
  insert into @mytab (val) 
  values (4)
 ..
END 

SELECT ...
from .... where ... in (select val from @mytab);

Open in new window

0
 
BlakeMcKennaAuthor Commented:
Thanks jogos...that's what I was looking for. My SQL programming skills aren't the best and I wasn't sure what the syntax should be!
0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now