Solved

Select id's from string - dynamic

Posted on 2014-04-15
1
209 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
[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
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

756 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