Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Formatting a SQL Statement in SQL Server?

Posted on 2014-11-13
4
Medium Priority
?
126 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

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…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

688 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