Solved

Formatting a SQL Statement in SQL Server?

Posted on 2014-11-13
4
97 Views
Last Modified: 2014-11-13
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
Comment
Question by:BlakeMcKenna
  • 2
4 Comments
 
LVL 65

Expert Comment

by:Jim Horn
Comment Utility
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
 
LVL 25

Expert Comment

by:jogos
Comment Utility
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
 
LVL 25

Accepted Solution

by:
jogos earned 500 total points
Comment Utility
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
 

Author Closing Comment

by:BlakeMcKenna
Comment Utility
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

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now