-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoopSelect.sql
41 lines (30 loc) · 1.04 KB
/
LoopSelect.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
DECLARE
@lstTableNames varchar (3000),
@Length int,
@StartPos int,
@Tablename varchar (100),
@NextPos int,
@SQL varchar (300)
SELECT @lstTableNames = 'TblAddress,tblAddressTelecom,tblAdvert,tblContact,tblOrganisation,tlkpCategory,tlkpCountry,tlkpDirectories,
tlkpOrganisationProduct,trelAddressBrandProduct,trelAddressContact,trelAdvertOrderItem,trelOrganisationCategory,trelOrganisationProduct,
trelOrganisationRelationship'
---set local variable to hold list loop data
SELECT @StartPos = 1,
@Length = LEN(@lstTableNames),
@NextPos = 0
---Begin Loop and continue until start of loop is greatter than its lenght
WHILE @StartPos <= @Length
BEGIN
SELECT @NextPos = CHARINDEX(',', @lstTableNames, @startpos)
IF @NextPos = 0
BEGIN
SET @NextPos = LEN(@lstTableNames) + 1
END
SELECT @Tablename = SUBSTRING(@lstTableNames, @StartPos, (@NextPos - @StartPos))
IF @Tablename <> ','
BEGIN
SET @SQL = 'SELECT * FROM ' + @Tablename
EXEC (@SQL)
END
SET @StartPos = @NextPos + 1
END