Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Formatting a SQL Statement in SQL Server?

Posted on 2014-11-13
4
Medium Priority
?
136 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 66

Expert Comment

by:Jim Horn
ID: 40440568
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
ID: 40440577
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 2000 total points
ID: 40440585
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
ID: 40440641
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

926 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