Solved

Formatting a SQL Statement in SQL Server?

Posted on 2014-11-13
4
105 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
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 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

856 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