Solved

Select id's from string - dynamic

Posted on 2014-04-15
1
205 Views
Last Modified: 2014-04-15
Hello,

Can I select specific rows from a table using a string? The items in the string are sent into a stored procedure and can contain various id's and I do not how many.
For example here are Id=1 and Id=3

DECLARE @MyString varchar(500)='1,3'

It could also be Id=1, Id=3 and Id=4
DECLARE @MyString varchar(500)='1,3,4'

DECLARE @MyTable table(Id int,MyString varchar(50))
INSERT INTO @MyTable(Id,MyString)values(1,'One')
INSERT INTO @MyTable(Id,MyString)values(2,'Two')
INSERT INTO @MyTable(Id,MyString)values(3,'Three')
INSERT INTO @MyTable(Id,MyString)values(4,'Four')
INSERT INTO @MyTable(Id,MyString)values(5,'Five')
0
Comment
Question by:johnson1
1 Comment
 
LVL 26

Accepted Solution

by:
Shaun Kline earned 500 total points
ID: 40001400
This article by Angeliii should help:
http://www.experts-exchange.com/Database/Miscellaneous/A_1536-delimited-list-as-parameter-what-are-the-options.html

It provides options for using a delimited parameter.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

929 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