Associating a variable with a tmptable - tsql

declare #count int = 0
while #count< 5
BEGIN
IF '#temptable%' = (SELECT name
FROM tempdb.sys.columns
WHERE column_name = 'Butter')
set #table1 = 'temptable%'
else '#temptable%' = (SELECT name
FROM tempdb.sys.columns
WHERE column_name = 'Parrot')
set #table2 = 'temptable%'
set #count= #count+ 1
END
So I have five temporary tables with the name #temptable1, #temptable2, etc. and would like to distinguish the them from each other and set them to a variable depending on the unique column_name (ie. #temptable1 will be associated with the variable #table1).
So is there a way I can call #table1 and then have it return #temptable1?

Related

Assign dynamic SQL Query result to a variable and then use variable to in UPDATE TABLE

Application: MS SSMS version: 18.12.1
Windows 10
I have Table_Name1 that contains the below info
col1Name
col2Name
col3Name
-,99
fred
1,23
col1 - col2 - col3 are all string datatype
col1 ordinal value = 1
col2 ordinal value = 2
col3 ordinal value = 3
I used the below code to retrieve the columns names from TABLE_NAME1 that were related to monetary values and placed those columns into TABLE_NAME2
DROP TABLE IF EXISTS STAGE.DBO.TABLE_NAME2<BR>
SELECT SC.NAME AS COLUMN_NAME<BR>
,SC.COLORDER AS COLUMN_ORDINAL<BR>
,CAST(ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS SMALLINT) AS ROW_NUM<BR>
INTO STAGE.DBO.TABLE_NAME2<BR>
FROM SYSOBJECTS AS SO<BR>
INNER JOIN SYSCOLUMNS AS SC ON SO.ID = SC.ID<BR>
WHERE SO.XTYPE = 'U'<BR>
AND SO.NAME = 'TABLE_NAME2'<BR>
AND SC.COLORDER IN (1,3)<BR>
ORDER BY SC.COLORDER, SC.NAME<BR>
TABLE_NAME2 contains the following info
column_name
column_ordinal
row_num
col1Name
1
1
col2Name
3
2
What I want to do: loop through TABLE_NAME1 column names and if there is a match with a value in TABLE_NAME2, then return that column name and assign it to a variable. Use the variable in an UPDATE TABLE statement to replace the comma (,) with a dot (.)
Below is what i tried: to have the result assigned to a variable
DECLARE #TABLE_NAME1 NVARCHAR(MAX)
DECLARE #TABLE_NAME2 NVARCHAR(MAX)
DECLARE #SQLQRY1 NVARCHAR(MAX)
DECLARE #LOOP1 SMALLINT
DECLARE #TABLE_ROW_COUNT1 SMALLINT
DECLARE #XTYPE1 CHAR(1)
DECLARE #COL_NAME1 NVARCHAR(MAX)
SET #TABLE_NAME1 = 'CH_IBRO_HOUSEHOLD_ACTIVITY_STG'
SET #TABLE_NAME2 = 'STAGE.DBO.CH_IBRO_HH_ACT_COL_NAME_STG'
SET #LOOP1 = 1
SET #XTYPE1 = 'U'
SET #TABLE_ROW_COUNT1 =2
WHILE #LOOP1 <= #TABLE_ROW_COUNT1
BEGIN
SET #SQLQRY1 = N'SELECT #COL_NAME1=TBL2.COLUMN_NAME
FROM ( SELECT SC.[NAME] AS COL_NAME, SC.COLORDER AS COL_ORDER
FROM SYSOBJECTS AS SO
INNER JOIN SYSCOLUMNS AS SC ON SO.ID = SC.ID
WHERE SO.XTYPE = '''+ #XTYPE1 +'''
AND SO.NAME = '''+ #TABLE_NAME1 +'''
) AS TBL1
INNER JOIN ( SELECT *
FROM '+ #TABLE_NAME2 +'
) AS TBL2 ON TBL2.COLUMN_NAME = TBL1.COL_NAME
AND TBL2.COLUMN_ORDINAL = TBL1.COL_ORDER
WHERE TBL2.ROW_NUM = ' + CAST(#LOOP1 AS NVARCHAR(MAX))
EXECUTE #COL_NAME1 = sp_executesql #SQLQRY1 OUTPUT
PRINT 'COL_NAME = '+ CAST(#COL_NAME1 AS VARCHAR);
PRINT 'LOOP NUMBER = '+ CAST(#LOOP1 AS VARCHAR);
/* INCREMENT */
SET #LOOP1 = #LOOP1 + 1
END
Thank you all for your support as I am new to T-SQL
thank you for the documentation. It helped alot. Just wanted to post most solution.
STEP1: CREATE TEMPORARY TABLE THAT WILL CONTIAN THE NAME OF THE COLUMNS THAT ARE SUPPOSE TO HAVE NUMERIC DATA
USE [STAGE]
DROP TABLE IF EXISTS STAGE.DBO.CH_IBRO_HOUSEHOLD_ACTIVITY_COLUMN_NAME_STG
SELECT SC.NAME AS COLUMN_NAME
,SC.COLORDER AS COLUMN_ORDINAL
,CAST(ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS SMALLINT) AS ROW_NUM
INTO STAGE.DBO.CH_IBRO_HOUSEHOLD_ACTIVITY_COLUMN_NAME_STG
FROM SYSOBJECTS AS SO
INNER JOIN SYSCOLUMNS AS SC ON SO.ID = SC.ID
WHERE SO.XTYPE = 'U'
AND SO.NAME = 'CH_IBRO_HOUSEHOLD_ACTIVITY_STG'
AND SC.COLORDER IN (26,27,28,29,30,31,32,33,34,35,36,39,40,45,46,47,48,49,50,54,55,56,59,60,61,64,65,66,83,84)
ORDER BY SC.COLORDER, SC.NAME
STEP2: APPLY GLOBAL SCRUBBING LOGIC TO THE COLUMNS CONTIAN NUMERIC DATA
USE [STAGE]
DECLARE #TABLE_NAME1 NVARCHAR(MAX)
DECLARE #TABLE_NAME2 NVARCHAR(MAX)
DECLARE #SQLQRY1 NVARCHAR(MAX)
DECLARE #LOOP1 SMALLINT
DECLARE #TABLE_ROW_COUNT1 SMALLINT
DECLARE #XTYPE1 CHAR(1)
DECLARE #COL_NAME1 NVARCHAR(MAX)
SET #TABLE_NAME1 = 'CH_IBRO_HOUSEHOLD_ACTIVITY_STG'
SET #TABLE_NAME2 = 'STAGE.DBO.CH_IBRO_HOUSEHOLD_ACTIVITY_COLUMN_NAME_STG'
SET #LOOP1 = 1
SET #XTYPE1 = 'U'
SET #TABLE_ROW_COUNT1 = (SELECT COUNT(*) FROM STAGE.DBO.CH_IBRO_HOUSEHOLD_ACTIVITY_COLUMN_NAME_STG)
DECLARE #ParmDefinition nvarchar(500);
SELECT #ParmDefinition = N'#COL_NAME1 NVARCHAR(MAX) OUTPUT';
WHILE #LOOP1 <= #TABLE_ROW_COUNT1
BEGIN
SET #SQLQRY1 = N'SELECT #COL_NAME1=TBL2.COLUMN_NAME
FROM ( SELECT SC.[NAME] AS COL_NAME, SC.COLORDER AS COL_ORDER
FROM SYSOBJECTS AS SO
INNER JOIN SYSCOLUMNS AS SC ON SO.ID = SC.ID
WHERE SO.XTYPE = '''+ #XTYPE1 +'''
AND SO.NAME = '''+ #TABLE_NAME1 +'''
) AS TBL1
INNER JOIN ( SELECT *
FROM '+ #TABLE_NAME2 +'
) AS TBL2 ON TBL2.COLUMN_NAME = TBL1.COL_NAME
AND TBL2.COLUMN_ORDINAL = TBL1.COL_ORDER
WHERE TBL2.ROW_NUM = ' + CAST(#LOOP1 AS NVARCHAR(MAX))
EXEC sp_executesql #SQLQRY1, #ParmDefinition, #COL_NAME1 OUTPUT
PRINT 'COL_NAME = '+ CAST(#COL_NAME1 AS VARCHAR);
PRINT 'LOOP NUMBER = '+ CAST(#LOOP1 AS VARCHAR);
/* REMOVE ALL LEADING AND TRAILING SPACES */
UPDATE STAGE.DBO.CH_IBRO_HOUSEHOLD_ACTIVITY_STG
SET #COL_NAME1 = LTRIM(RTRIM(#COL_NAME1))
/* REMOVE ALL ASCII CHAR (13) PRESS ENTER */
UPDATE STAGE.DBO.CH_IBRO_HOUSEHOLD_ACTIVITY_STG
SET #COL_NAME1 = REPLACE(#COL_NAME1,CHAR(13),'')
/* REMOVE ALL ASCII CHAR (10) LINE FEED */
UPDATE STAGE.DBO.CH_IBRO_HOUSEHOLD_ACTIVITY_STG
SET #COL_NAME1 = REPLACE(#COL_NAME1,CHAR(10),'')
/* REMOVE ALL ASCII CHAR (9) HORIZONTAL TAB */
UPDATE STAGE.DBO.CH_IBRO_HOUSEHOLD_ACTIVITY_STG
SET #COL_NAME1 = REPLACE(#COL_NAME1,CHAR(9),'')
/* REMOVE ALL LEADING AND TRAILING SPACES - 2ND PASS */
UPDATE STAGE.DBO.CH_IBRO_HOUSEHOLD_ACTIVITY_STG
SET #COL_NAME1 = LTRIM(RTRIM(#COL_NAME1))
/* INCREMENT */
SET #LOOP1 = #LOOP1 + 1
END

How to pivot a table to a view on matching-length delimited cells

Disclaimer: I'm dealing with a rather old legacy system so any comments telling me about poor design are redundant, although I do genuinely appreciate any such sentiment. There is a new version that solves most legacy problems but we still have to maintain the old system, so basically, we have to manage for now.
I have a table that looks like this (yes, that is a single column, I know):
And I need a view (for reporting purposes) that will dynamically process the data in said table and return this:
The values are \n-delimited (shudder) and you can assume there will always be the same number of values in each cell (9 in the example, although other databases could have 4 or 12 or any number), although I suppose having NULL-insertion in the event of missing values couldn't hurt. They will also always be in a matching order (as in the example, 'AUD', 'Australian Dollar', and '$' are all the first values in their respective cells, and so on).
I've found various approaches to splitting a single cell out into a view, but nothing that covers merging data in such a way as I require. Sitting at home with a cold has not helped my research capabilities. Help me StackOverflow, you're my only hope!
Bonus points for tidy, relatively readable SQL examples, although I'm anticipating messiness as a natural by-product of the hackish nature of my required solution.
Something like this. I didn't take the time to build out the tables, but it should be fairly obvious where you can replace my variables with your rows. You will also want to do a replace char(10) where I have used commas. You could package it up in a table valued function and then call as a view.
declare #xml1 xml
declare #xml2 xml
declare #xml3 xml
declare #c1 nvarchar(250)
declare #c2 nvarchar(250)
declare #c3 nvarchar(250)
set #c1 = N'AUD,CAD,EUR,GBP,JPY,NZD,USD,KES,CHF';
set #c2 = N'Australian Dollar,Canadian Dollar,Euro,Pound Sterling,Yen,New Zealand Dollar,United States Dollar,Kenyan Shilling, Swiss Franc';
set #c3 = N'$,$,C,L,Y,$,$,K,F';
-- you'd use replace(#c1, char(10), '</r><r>') etc etc for /n delimited code
set #xml1 = N'<root><r>' + replace(#c1,',','</r><r>') + '</r></root>';
set #xml2 = N'<root><r>' + replace(#c2,',','</r><r>') + '</r></root>';
set #xml3 = N'<root><r>' + replace(#c3,',','</r><r>') + '</r></root>';
select code.code, name.name, symbol.symbol
from
(select ROW_NUMBER() over (order by ##rowcount) as ck,
c.value('.','varchar(max)') as [code]
from #xml1.nodes('//root/r') as a(c)) as code
inner join
(select ROW_NUMBER() over (order by ##rowcount) as nk,
n.value('.','varchar(max)') as [name]
from #xml2.nodes('//root/r') as a(n)) as name on code.ck = name.nk
inner join
(select ROW_NUMBER() over (order by ##rowcount) as sk,
s.value('.','varchar(max)') as [symbol]
from #xml3.nodes('//root/r') as a(s)) as symbol on symbol.sk = name.nk
You can run this as a single script in SSMS for verification that it works. No schema necessary.
Using Jeff Moden's Tally Ho! CSV splitter:
CREATE FUNCTION [dbo].[DelimitedSplit8K]
--===== Define I/O parameters
(#pString VARCHAR(8000), #pDelimiter CHAR(1))
--WARNING!!! DO NOT USE MAX DATA-TYPES HERE! IT WILL KILL PERFORMANCE!
RETURNS TABLE WITH SCHEMABINDING AS
RETURN
--===== "Inline" CTE Driven "Tally Table" produces values from 1 up to 10,000...
-- enough to cover VARCHAR(8000)
WITH
E1(N) AS (
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
), --10E+1 or 10 rows
E2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rows
E4(N) AS (SELECT 1 FROM E2 a, E2 b), --10E+4 or 10,000 rows max
cteTally(N) AS (--==== This provides the "base" CTE and limits the number of rows right up front
-- for both a performance gain and prevention of accidental "overruns"
SELECT TOP (ISNULL(DATALENGTH(#pString),0)) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4
),
cteStart(N1) AS (--==== This returns N+1 (starting position of each "element" just once for each delimiter)
SELECT 1 UNION ALL
SELECT t.N+1 FROM cteTally t WHERE SUBSTRING(#pString,t.N,1) = #pDelimiter
),
cteLen(N1,L1) AS(--==== Return start and length (for use in substring)
SELECT s.N1,
ISNULL(NULLIF(CHARINDEX(#pDelimiter,#pString,s.N1),0)-s.N1,8000)
FROM cteStart s
)
--===== Do the actual split. The ISNULL/NULLIF combo handles the length for the final element when no delimiter is found.
SELECT ItemNumber = ROW_NUMBER() OVER(ORDER BY l.N1),
Item = SUBSTRING(#pString, l.N1, l.L1)
FROM cteLen l
;
and inline CTE data like this
with
data as (select Num,Currencies from (values
(1,'AUD'+char(10)+'CAD'+char(10)+'USD'+char(10)+'KES')
,(2,'Australian DOllar'+char(10)+'Canadian Dollar'+char(10)+'US Dollar'+char(10)+'Kenyan Shilling')
,(3,'$'+char(10)+'$'+char(10)+'$'+char(10)+'k')
)data(Num,Currencies)
),
The solution is as simple as this:
map as (select * from (values
(1,'Code')
,(2,'Name')
,(3,'Symbol')
)map(Num,Col )
)
select
ItemNumber
,max(Code) as Code
,max(Name) as Name
,max(Symbol) as Symbol
from (
select
map.Num
,map.Col
,c.Item
,c.ItemNumber
from data
join map
on map.Num = data.Num
cross apply dbo.DelimitedSplit8K(data.Currencies,char(10)) c
) t
pivot (max(Item) for Col in (Code,Name,Symbol)) pvt
group by ItemNumber
to give us:
ItemNumber Code Name Symbol
-------------- ---- -------------------- ---------------
1 AUD Australian DOllar $
2 CAD Canadian Dollar $
3 USD US Dollar $
4 KES Kenyan Shilling k
Hope this Helps. Run all together or replace the table variable with a temptable.
Sample Data:
IF OBJECT_ID(N'tempdb..#table') > 0
BEGIN
DROP TABLE #table
END
DECLARE #table TABLE(ATTRIBUTELVAUE VARCHAR(MAX))
INSERT INTO #table
SELECT
'AFN
ALL
DZD
USD
EUR
AOA
XCD
XCD
ARS'
INSERT INTO #table
SELECT
'Afghanistan
Albania
Algeria
American Samoa
Andorra
Angola
Anguilla
Antigua and Barbuda
Argentina'
INSERT INTO #table
SELECT
'AF
AL
DZ
AS
AD
AO
AI
AG
AR'
Query:
IF OBJECT_ID(N'tempdb..#TEMP') > 0
BEGIN
DROP TABLE #TEMP
END
DECLARE #StartLoop INT
DECLARE #EndLoop INT
DECLARE #Code TABLE (ID INT IDENTITY(1, 1),
Code VARCHAR(250))
DECLARE #Name TABLE (ID INT IDENTITY(1, 1),
Name VARCHAR(250))
DECLARE #Symbol TABLE (ID INT IDENTITY(1, 1),
Symbol VARCHAR(250))
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS ID,
*
INTO #Temp
FROM #table
SELECT #StartLoop = MIN(ID),
#EndLoop = MAX(ID)
FROM #Temp
WHILE #StartLoop <= #EndLoop
BEGIN
DECLARE #WorkingString VARCHAR(MAX)
SELECT #WorkingString = ATTRIBUTELVAUE + CHAR(10) + ' '
FROM #Temp
WHERE ID = #StartLoop
--print #WorkingString
WHILE CHARINDEX(CHAR(10), #WorkingString) > 0
BEGIN
DECLARE #SearchCharacter INT
DECLARE #WorkingStringLength INT
DECLARE #TempStringLength INT
DECLARE #TempString VARCHAR(MAX)
SET #WorkingStringLength = LEN(#WorkingString)
SET #SearchCharacter = CHARINDEX(CHAR(10), #WorkingString)
SET #TempString = SUBSTRING(#WorkingString, 1, #SearchCharacter - 1)
SET #TempStringLength = LEN(#TempString)
SET #WorkingString = SUBSTRING(#WorkingString, #SearchCharacter + 1, #WorkingStringLength)
SET #TempString = REPLACE(#TempString, CHAR(13), '')
IF #StartLoop = 1
BEGIN
INSERT INTO #Code
SELECT #TempString
END
IF #StartLoop = 2
BEGIN
INSERT INTO #Name
SELECT #TempString
END
IF #StartLoop = 3
BEGIN
INSERT INTO #Symbol
SELECT #TempString
END
END
SET #StartLoop = #StartLoop + 1
END
SELECT Code,
Name,
Symbol
FROM #Code AS c
JOIN #Name AS n
ON c.ID = n.ID
JOIN #Symbol AS s
ON s.ID = n.ID
Cleanup:
IF OBJECT_ID(N'tempdb..#TEMP') > 0
BEGIN
DROP TABLE #TEMP
END
IF OBJECT_ID(N'tempdb..#table') > 0
BEGIN
DROP TABLE #table
END
Because I needed a view, this ended up being my solution:
CREATE FUNCTION [dbo].[CurrencyTableGenerator]()
RETURNS
#CurrencyTable TABLE(
Code NVARCHAR(250)
,Name NVARCHAR(250)
,Symbol NVARCHAR(250)
)
AS
BEGIN
DECLARE #xml1 XML
DECLARE #xml2 XML
DECLARE #xml3 XML
DECLARE #C1 NVARCHAR(250)
DECLARE #C2 NVARCHAR(250)
DECLARE #c3 NVARCHAR(250)
SET #c1 = (SELECT ...)
SET #c2 = (SELECT ...)
SET #c3 = (SELECT ...)
SET #xml1 = N'<root><r>' + REPLACE(#c1, CHAR(10), '</r><r>') + '</r></root>';
SET #xml2 = N'<root><r>' + REPLACE(#c2, CHAR(10), '</r><r>') + '</r></root>';
SET #xml3 = N'<root><r>' + REPLACE(#c3, CHAR(10), '</r><r>') + '</r></root>';
INSERT INTO #CurrencyTable
SELECT Code.Code, Name.Name, Symbol.Symbol
FROM
(SELECT ROW_NUMBER() OVER (ORDER BY ##ROWCOUNT) AS ck,
c.value('.', 'VARCHAR(250)') AS [Code]
FROM #xml1.nodes('//root/r') AS a(c)) AS Code
INNER JOIN
(SELECT ROW_NUMBER() OVER (ORDER BY ##ROWCOUNT) AS nk,
n.value('.', 'VARCHAR(250)') AS [Name]
FROM #xml2.nodes('//root/r') AS a(n)) AS Name ON Code.ck = Name.nk
INNER JOIN
(SELECT ROW_NUMBER() OVER (ORDER BY ##ROWCOUNT) AS sk,
s.value('.', 'VARCHAR(250)') AS [Symbol]
FROM #xml3.nodes('//root/r') AS a(s)) AS Symbol ON Symbol.sk = Name.nk
RETURN
END
GO
CREATE VIEW [dbo].[CurrencyView]
AS
SELECT * FROM [dbo].[CurrencyTableGenerator]()
GO
Thanks to RThomas for the function.

SQL Server 2008 T-SQL UDF odds and ends

I am trying to take a data string from one column and split it into several different columns in SQL Ser 2008. Eample: Name Account 445566 0010020056893010445478008 AFD 369. I'm useing a borrowed space delimited split function which workS great. The problem is I'm new to T-SQL and have a few questions.
How do I get the function to run through a full table not just a string literal?
This generates a temparary table how do you take these values and insert them into my table? Is it just an insert statement?
Here is the script and usage:
CREATE FUNCTION [dbo].[Split]
(
#String varchar(max)
,#Delimiter char
)
RETURNS #Results table
(
Ordinal int
,StringValue varchar(max)
)
as
begin
set #String = isnull(#String,'')
set #Delimiter = isnull(#Delimiter,'')
declare
#TempString varchar(max) = #String
,#Ordinal int = 0
,#CharIndex int = 0
set #CharIndex = charindex(#Delimiter, #TempString)
while #CharIndex != 0 begin
set #Ordinal += 1
insert #Results values
(
#Ordinal
,substring(#TempString, 0, #CharIndex)
)
set #TempString = substring(#TempString, #CharIndex + 1, len(#TempString) - #CharIndex)
set #CharIndex = charindex(#Delimiter, #TempString)
end
if #TempString != '' begin
set #Ordinal += 1
insert #Results values
(
#Ordinal
,#TempString
)
end
return
end
--USAGE
select
s.*
from dbo.Split('Name Account 445566 0010020056893010445478008 AFD 369', ' ') as s
where rtrim(s.StringValue) != ''
GO
To use a table valued udf against a table, you need CROSS APPLY (or maybe OUTER APPLY depending on how you want to deal with "no rows" from the udf). This applies the row-by-row operation of the udf against your table which itself is a table
SELECT
*
FROM
mytable M
CROSS APPLY
[dbo].[Split] (M.TheColumn) S
To INSERT
INSERT AnotherTable (col1, col2, ...)
SELECT
col1, col2, ...
FROM
mytable M
CROSS APPLY
[dbo].[Split] (M.TheColumn) S

TSQL Hack needed for getting a filter for data

A UI (before the report shows) shows a look up (Combo) that has
(ID = 0).All Organization Units
(ID =4).HR
(ID = 5).DEV
I need to:
Be able to show data of (4) + (5) if
(0) is selected.
Only (4) OR (5) if either HR or DEV is selected.
Lookup combo code (Selected Feeds the parameter in the below query.)
Select 0 AS ID,'All Org' AS Name from DP_ORG_OrganizationUnit
where DP_ORG_OrganizationUnit.Code IN {AccessData}
Union
SELECT
DP_ORG_OrganizationUnit.ID,
DP_ORG_OrganizationUnit.Name
FROM DP_ORG_OrganizationUnit where DP_ORG_OrganizationUnit.Code IN ('HR','DEV')
Report data row query
SET CONCAT_NULL_YIELDS_NULL OFF
DECLARE #EmpID as int;
DECLARE #OrganizationUnit as int;
DECLARE #StartDate as datetime;
DECLARE #EndDate as datetime;
SET #EmpID = ?;
SET #StartDate = ?;
SET #EndDate = ?;
SET #OrganizationUnit = ?;
SELECT
Employee.Code,
Employee.Name1+' '+Employee.Name2+' '+Employee.Name3+' '+Employee.Name4+' '+Employee.Name5 AS FullName,
Employee.OrganizationUnit,
ContractType.Name,
EmployeeContract.StartDate,
EmployeeContract.EndDate
FROM Employee INNER JOIN (ContractType INNER JOIN EmployeeContract
ON ContractType.ID = EmployeeContract.ContractType)
ON Employee.ID = EmployeeContract.Employee
WHERE (Employee.ID = #EmpID OR #EmpID=0)
AND
(Employee.OrganizationUnit = #OrganizationUnit OR #OrganizationUnit=0)
AND NOT((EndDate < #StartDate or StartDate > #EndDate));
Any way I can achieve it from the looks of it? 0=0 would show all the data from other
departments too..
Anybody :-o?
First off, your lookup combo code could be tightened up a bit:
-- the FROM clause was superfluous
SELECT 0 AS ID,'All Org' AS Name
UNION ALL
-- the two-part identifiers were superfluous (only one table)
SELECT ID, Name
FROM DP_ORG_OrganizationUnit
WHERE Code IN ('HR','DEV')
For the report query, the simplest form would be:
WHERE
((#OrganizationUnit > 0 AND Employee.OrganizationUnit = #OrganizationUnit) OR
(#OrganizationUnit = 0 AND Employee.OrganizationUnit IN (4,5)))
something like this should work
Where (Employee.OrganizationUnit = case when #OrganizationUnit=0 then 4 else #OrganizationUnit end OR case when #OrganizationUnit=0 then 5 else #OrganizationUnit end)
Try this, which should use indexes on your query...
DECALRE #FilterValues (FilterValue int not null primary key)
IF #Param=0
BEGIN
INSERT INTO #FilterValues VALUES (4)
INSERT INTO #FilterValues VALUES (5)
END
ELSE ID #PAram IS NOT NULL
BEGIN
INSERT INTO #FilterValues VALUES (#Param)
END
SELECT
....
FROM YourTable y
INNER JOIN #FilterValues f ON y.Value=f.Value
WHERE .....
KM's version will work, but this query does not need a temp table...
SELECT *
FROM Employee
WHERE (
#OrganizationUnit = 0
OR
(
#OrganizationUnit <> 0
AND
Employee.OrganizationUnit = #OrganizationUnit
)
)
How about
WHERE (Employee.ID = #EmpID OR #EmpID=0)
AND
(Employee.OrganizationUnit BETWEEN ISNULL(NULLIF(#OrganizationUnit,0),0) AND ISNULL(NULLIF(#OrganizationUnit,0),99))
AND NOT((EndDate < #StartDate or StartDate > #EndDate));

Implementing and applying a string split in T-SQL

I have this statement in T-SQL.
SELECT Bay From TABLE where uid in (
select B_Numbers from Info_Step WHERE uid = 'number'
)
I am selecting "multiple" BAYs from TABLE where their uid is equal to a string of numbers like this:
B_Numbers = 1:45:34:98
Therefore, I should be selecting 4 different BAYs from TABLE. I basically need to split the string 1:45:34:98 up into 4 different numbers.
I'm thinking that Split() would work, but it doesn't and I get a syntax error.
Any thoughts from the T-SQL gods would be awesome!
Here is an implementation of a split function that returns the list of numbers as a table:
http://rbgupta.blogspot.com/2007/03/split-function-tsql.html
Looks like this would set you on your way...
Here is a method that uses an auxiliary numbers table to parse the input string. The logic can easily be added to a function that returns a table. That table can then be joined to lookup the correct rows.
Step 1: Create the Numbers table
SET NOCOUNT ON
GO
IF EXISTS
(
SELECT 1
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'Numbers'
AND TABLE_SCHEMA = 'dbo'
AND TABLE_TYPE = 'BASE TABLE'
)
BEGIN
DROP TABLE dbo.Numbers
END
GO
CREATE TABLE dbo.Numbers
(
Number smallint IDENTITY(1, 1) PRIMARY KEY
)
GO
WHILE 1 = 1
BEGIN
INSERT INTO dbo.Numbers DEFAULT VALUES
IF SCOPE_IDENTITY() = 32767
BEGIN
BREAK
END
END
GO
Step 2: Parse the Input String
CREATE FUNCTION dbo.ParseString(#input_string varchar(8000), #delim varchar(8000) = " ")
RETURNS TABLE
AS RETURN
(
SELECT Number
FROM dbo.Numbers
WHERE CHARINDEX
(
#delim + CONVERT(VARCHAR(12),Number) + #delim,
#delim + #input_string + #delim
) > 0
)
GO
**EXAMPLE**
SELECT * FROM dbo.ParseString('1:45:34:98',':')
Step 3: Use the results however you want/need
Number
------
1
34
45
98
End-To-End Example
Create function that returns the appropriate BNumber (of course change it to use the commented out SQL)
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION dbo.GetBNumber (#uid int)
RETURNS VARCHAR(8000)
AS
BEGIN
RETURN '1:45:34:98'
--select B_Numbers from Info_Step WHERE uid = #uid
END
GO
Use the use functions to return the desired results
-- Using Test Data
SELECT N.Number FROM Numbers N
JOIN dbo.ParseString(dbo.GetBNumber(12345),':') Q ON Q.Number = N.Number
-- Using Your Data (Untested but should work.)
SELECT N.Bay
FROM TABLE N
JOIN dbo.ParseString(dbo.GetBNumber(ENTER YOU NUMBER HERE),':') Q ON Q.Number = N.uid
Results
Number
------
1
34
45
98
You should keep your arrays as rows but if I understand your question I think this will work.
SELECT
Bay
From
TABLE
join Info_Step
on B_Numbers like '%'+ uid +'%'
where
Info_Step.uid = 'number'
This query will do a full table scan because of the like operator.
What you can do is loop through the B_Numbers entries and do your own split on : Insert those entries into a temp table and then perform your query.
DECLARE #i int
DECLARE #start int
DECLARE #B_Numbers nvarchar(20)
DECLARE #temp table (
number nvarchar(10)
)
-- SELECT B_Numbers FROM Info_Step WHERE uid = 'number'
SELECT #B_Numbers = '1:45:34:98'
SET #i = 0
SET #start = 0
-- Parse out characters delimited by ":";
-- Would make a nice user defined function.
WHILE #i < len(#B_Numbers)
BEGIN
IF substring(#B_Numbers, #i, 1) = ':'
BEGIN
INSERT INTO #temp
VALUES (substring(#B_Numbers, #start, #i - #start))
SET #start = #i + 1
END
SET #i = #i + 1
END
-- Insert last item
INSERT INTO #temp
VALUES (substring(#B_Numbers, #start, #i - #start + 1))
-- Do query with parsed values
SELECT Bay FROM TABLE WHERE uid in (SELECT * FROM #temp)
You can even try this
declare #str varchar(50)
set #str = '1:45:34:98'
;with numcte as(
select 1 as rn union all select rn+1 from numcte where rn<LEN(#str)),
getchars as(select
ROW_NUMBER() over(order by rn) slno,
rn,chars from numcte
cross apply(select SUBSTRING(#str,rn,1) chars)X where chars = ':')
select top 1
Bay1 = SUBSTRING(#str,0,(select rn from getchars where slno = 1))
,Bay2 = SUBSTRING(#str,
(select rn from getchars where slno = 1) + 1,
(((select rn from getchars where slno = 2)-
(select rn from getchars where slno = 1)
)-1))
,Bay3 = SUBSTRING(#str,
(select rn from getchars where slno = 2) + 1,
(((select rn from getchars where slno = 3)-
(select rn from getchars where slno = 2)
)-1))
,Bay4 = SUBSTRING(#str,
(select rn from getchars where slno = 3)+1,
LEN(#str))
from getchars
Output:
Bay1 Bay2 Bay3 Bay4
1 45 34 98