SQL join data row by level - sql

I have table like this
level|value
1 |ABC
1 |XYZ
1 |QWER
2 |1234
2 |7360
3 |zxcv
3 |0001
How can I join each value on level 1 to all level below? Like:
ABC-1234-zxcv
ABC-1234-0001
ABC-7360-zxcv
...

It the number of levels is not fixed:
Declare #select varchar(max) = 'SELECT ',
#from varchar(max) = 'FROM ',
#where varchar(max) = 'WHERE ',
#query varchar(max)= '';
SELECT #select = #select + 't' + cast([level] as varchar(max)) + '.[value]+''-''+',
#from = #from + 'yourTable t' + cast([level] as varchar(max)) + ',',
#where = #where + 't' + cast([level] as varchar(max)) + '.[level] = ' + cast([level] as varchar(max)) + ' AND '
FROM yourTable
GROUP BY [level]
Set #query = SUBSTRING(#select, 1, len(#select) - 5) + ' ' +
SUBSTRING(#from, 1, len(#from) - 1) + ' ' +
SUBSTRING(#where, 1, len(#where) - 4) + ' ORDER BY 1'
EXEC(#query)

If you have always 3 levels, you can do it like this:
select
d1.value + '-' + d2.value + '-' + d3.value
from
data d1
cross join data d2
cross join data d3
where
d1.level = 1 and
d2.level = 2 and
d3.level = 3
order by
1
If the number of levels isn’t fixed, then you'll probably have to use a recursive CTE

Related

Msg 156, Level 15, State 1, Line 1 Incorrect syntax near the keyword 'SELECT'. Msg 102, Level 15, State 1, Line 1 Incorrect syntax near '.'

I try to run 2 dynamic SQL, but I have an error that the syntax is incorrect. When I make a select for every parameter that I declare, there is no problem. But when I try to execute the string I have these erroes. Any help?
Msg 156, Level 15, State 1, Line 1 Incorrect syntax near the keyword 'SELECT'. Msg 102, Level 15, State 1, Line 1 Incorrect syntax near '.'
--declare Variablen für die Prozedur
declare #DispoColumn nvarchar(max) = (select STRING_AGG(col.name, ', ') within group (order by col.column_id)
FROM sys.objects obj
JOIN sys.columns col on col.object_id = obj.object_id
JOIN sys.types typ ON col.user_type_id=typ.user_type_id
Where obj.name = #Tabelle and obj.schema_id = 5
GROUP BY obj.name);
declare #ID nvarchar(150) = (select MAX(CASE WHEN col.column_id = 1 THEN col.name ELSE NULL END)
FROM sys.objects obj
JOIN sys.columns col on col.object_id = obj.object_id
JOIN sys.types typ ON col.user_type_id=typ.user_type_id
Where obj.name = #Tabelle and obj.schema_id = 5
GROUP BY obj.name);
declare #EFREdispoUpdate nvarchar(max)
declare #ESFdispoUpdate nvarchar(max)
declare #ESFdispoInsert nvarchar(max)
declare #EFREdispoInsert nvarchar(max)
--Update der Tabellen
set #ESFdispoUpdate = 'UPDATE ESF.' + #Tabelle + ' SET GUELTIG_BIS = GETDATE() WHERE GUELTIG_BIS = ''9999-12-31'' AND ' + #ID + ' IN ( SELECT '
+ #ID + 'FROM ( SELECT ' +#DispoColumn + ' FROM ESF.' + #Tabelle + ' WHERE GUELTIG_BIS = ''9999-12-31''' + ' EXCEPT SELECT ' + #DispoColumn + ' SF.' + #Tabelle + ') as alt );'
+ ' UPDATE DISPO.T_TABELLEN SET ANZ_GEANDERT = (SELECT COUNT(*) FROM ESF.'+#Tabelle + ' WHERE GUELTIG_BIS = GETDATE())'
+ 'WHERE SCH_NAME = ''ESF'' AND TAB_NAME = ''' +#Tabelle + ''';';
set #EFREdispoUpdate = 'UPDATE EFRE.' + #Tabelle + ' SET GUELTIG_BIS = GETDATE() WHERE GUELTIG_BIS = ''9999-12-31'' AND ' + #ID + ' IN ( SELECT '
+ #ID + 'FROM ( SELECT ' +#DispoColumn + ' FROM EFRE.' + #Tabelle + ' WHERE GUELTIG_BIS = ''9999-12-31''' + ' EXCEPT SELECT ' + #DispoColumn + ' SF.' + #Tabelle + ') as alt );'
+ ' UPDATE DISPO.T_TABELLEN SET ANZ_GEANDERT = (SELECT COUNT(*) FROM EFRE.'+#Tabelle + ' WHERE GUELTIG_BIS = GETDATE())'
+ 'WHERE SCH_NAME = ''EFRE'' AND TAB_NAME = ''' +#Tabelle + ''';';
EXEC sp_executesql #ESFdispoUpdate;
EXEC sp_executesql #EFREdispoUpdate;
Your issue is because on the second line of your set statements, you forgot a space before FROM
Other minor changes
Added brackets to column names just in case column has special character like a space
Removed unnecessary GROUP BY clauses in subqueries
Changed second subquery from CASE WHEN to WHERE so it's more intuitive
DECLARE #Tabelle NVARCHAR(100) = 'YourTable'
declare #DispoColumn nvarchar(max) = (select STRING_AGG(QUOTENAME(col.name), ', ') within group (order by col.column_id)
FROM sys.objects obj
JOIN sys.columns col on col.object_id = obj.object_id
WHERE obj.name = #Tabelle AND obj.schema_id = 5
);
declare #ID nvarchar(150) = (SELECT QUOTENAME(col.name)
FROM sys.objects obj
JOIN sys.columns col on col.object_id = obj.object_id
Where obj.name = #Tabelle AND obj.schema_id = 5
AND col.column_id = 1
);
select #DispoColumn,#ID
declare #EFREdispoUpdate nvarchar(max)
,#ESFdispoUpdate nvarchar(max)
,#ESFdispoInsert nvarchar(max)
,#EFREdispoInsert nvarchar(max)
set #ESFdispoUpdate = 'UPDATE ESF.' + #Tabelle + ' SET GUELTIG_BIS = GETDATE() WHERE GUELTIG_BIS = ''9999-12-31'' AND ' + #ID + ' IN ( SELECT '
+ #ID + ' FROM ( SELECT ' +#DispoColumn + ' FROM ESF.' + #Tabelle + ' WHERE GUELTIG_BIS = ''9999-12-31''' + ' EXCEPT SELECT ' + #DispoColumn + ' SF.' + #Tabelle + ') as alt );'
+ ' UPDATE DISPO.T_TABELLEN SET ANZ_GEANDERT = (SELECT COUNT(*) FROM ESF.'+#Tabelle + ' WHERE GUELTIG_BIS = GETDATE())'
+ 'WHERE SCH_NAME = ''ESF'' AND TAB_NAME = ''' +#Tabelle + ''';';
set #EFREdispoUpdate = 'UPDATE EFRE.' + #Tabelle + ' SET GUELTIG_BIS = GETDATE() WHERE GUELTIG_BIS = ''9999-12-31'' AND ' + #ID + ' IN ( SELECT '
+ #ID + ' FROM ( SELECT ' +#DispoColumn + ' FROM EFRE.' + #Tabelle + ' WHERE GUELTIG_BIS = ''9999-12-31''' + ' EXCEPT SELECT ' + #DispoColumn + ' SF.' + #Tabelle + ') as alt );'
+ ' UPDATE DISPO.T_TABELLEN SET ANZ_GEANDERT = (SELECT COUNT(*) FROM EFRE.'+#Tabelle + ' WHERE GUELTIG_BIS = GETDATE())'
+ 'WHERE SCH_NAME = ''EFRE'' AND TAB_NAME = ''' +#Tabelle + ''';';
/*Use for debugging*/
SELECT #ESFdispoUpdate
SELECT #EFREdispoUpdate

problem in executing dynamic query using pivot with syntax error Incorrect syntax near ' + '

I want to Create a query with dynamic query and in this query i use concat some cells , when use concat like 'N'subject:' + ' ' + ContentProductionTitle' i get syntax error
error : Incorrect syntax near ' + '.
DECLARE #cols AS NVARCHAR(MAX),
#query AS NVARCHAR(MAX);
SET #cols = STUFF((SELECT distinct ',' + QUOTENAME(c.KeyWordTitle)
FROM TBL_CU_ContentProduction_KeyWord c
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set #query = 'SELECT ' + #cols + ' from
(
select
isnull((select N'subject:' + ' ' + ContentProductionTitle
from Tbl_Cu_ContentProductionCalendar
where ContentProductionCalendarID = a.ActionSubjectId ),'')
+' ' +
isnull((select top 1 N'author:'+ p.FullName + ' ' from users.TblProfiles as p
where p.UserId= a.ActionUserID ),'') +' ' +
isnull((select top 1 N'edit bye:' + ' ' + p.FullName from users.TblProfiles as p
where p.UserId= a.CheckAgain ) ,'') as title
, a.ActionDateTo
, k.KeyWordTitle
from TBL_CU_ContentProduction_KeyWord as k
inner join Tbl_CU_ContentProduction as cp
on k.ContentGUID = cp.ContentGUID
inner join Tbl_Cu_ActionContentProduction as a
on a.ContentGUID = cp.ContentGUID
) x
pivot
(
max(ActionDateTo)
for KeyWordTitle in (' + #cols + ')
) p '
execute(#query)
you need to escape single quote inside your query string with '':
set #query = 'SELECT ' + #cols + ' from
(
select
isnull((select N''subject:'' + '' '' + ContentProductionTitle
from Tbl_Cu_ContentProductionCalendar
where ContentProductionCalendarID = a.ActionSubjectId ),'''')
+'' '' +
isnull((select top 1 N''author:''+ p.FullName + '' '' from users.TblProfiles as p
where p.UserId= a.ActionUserID ),'''') +'' '' +
isnull((select top 1 N''edit bye:'' + '' '' + p.FullName from users.TblProfiles as p
where p.UserId= a.CheckAgain ) ,'''') as title
, a.ActionDateTo
, k.KeyWordTitle
from TBL_CU_ContentProduction_KeyWord as k
inner join Tbl_CU_ContentProduction as cp
on k.ContentGUID = cp.ContentGUID
inner join Tbl_Cu_ActionContentProduction as a
on a.ContentGUID = cp.ContentGUID
) x
pivot
(
max(ActionDateTo)
for KeyWordTitle in (' + #cols + ')
) p '

How to compare data from multiple databases

I'm trying to compare some data from different multiple databases, as I have illustrate my current case, I have there databases, database 1 is the main, and time to time database 2 and database 3 are updated from database 1. I have some difficulties to get the final result which return the data from database 1 and two columns column show the availability in database 2 as Yes or No, and the same with second extra column that will indicate the data availability on the database 3 with Yes or NO.
SELECT *
FROM (
Select ID as db1_ID,
First_name as db1_First_name,
Last_name as db1_Last_name,
Email as db1_Email,
Password as db1_Password,
Request_Id as db1_Request_Id,
User_Id as db1_User_Id,
Request_name as db1_Request_name
from User
inner join User_request
on User_request.User_Id = user.ID
) AS DB1_VIEW
LEFT OUTER JOIN
(
Select ID as db2_ID,
First_name as db2_First_name,
Last_name as db2_Last_name,
Email as db2_Email,
Password as db2_Password,
Request_Id as db2_Request_Id,
User_Id as db2_User_Id,
Request_name as db2_Request_name
from User
inner join User_request
on User_request.User_Id = user.ID
) AS DB2_VIEW
ON db2_ID = db1_ID
LEFT OUTER JOIN
(
Select ID as db3_ID,
First_name as db3_First_name,
Last_name as db3_Last_name,
Email as db3_Email,
Password as db3_Password,
Request_Id as db3_Request_Id,
User_Id as db3_User_Id,
Request_name as db3_Request_name
from User
inner join User_request
on User_request.User_Id = user.ID
) AS DB3_VIEW
ON db3_ID = db1_ID
ID First_name Last_name Email Password Request_Id User_Id Request_name
1 Oliver Jake OJake#domain.com 123 1 1 Request1
2 Mathew Harry MHarry#domain.com 123 1 2 Request1
3 Jacob Reece JReece#domain.com 123 1 3
Request1
4 Charlie Damian CDamian#domain.com 123 1 4 Request1
Use this as your first select statement:
SELECT DB1_VIEW.*
,CASE WHEN DB2_VIEW.db2_ID IS NOT NULL THEN 'Y' ELSE 'N' END AS Available_db2
,CASE WHEN DB3_VIEW.db3_ID IS NOT NULL THEN 'Y' ELSE 'N' END AS Available_db3
You can remove all the details apart from the ID fields in the db2_view and db3_view subqueries.
You can use the below query before execute you should use replace [SourceDB] to your source database and [TargertDB] to your target database. Insert the table name into #mdtables to include for comparison.
USE [SourceDB]
IF Object_id('tempdb..#mdTables') IS NOT NULL
DROP TABLE #mdtables;
CREATE TABLE #mdtables
(
id INT IDENTITY(1, 1) NOT NULL,
schemaname NVARCHAR(128),
tablename NVARCHAR(128)
);
INSERT INTO #mdtables
(schemaname,
tablename)
VALUES ('dbo',
'user');
DECLARE #mdTableLim INT =0,
#mdTableRowId INT =0
SELECT #mdTableLim = Count(*)
FROM #mdtables;
SET #mdTableRowId = 1;
WHILE #mdTableRowId <= #mdTableLim
BEGIN
DECLARE #SDBName VARCHAR(50) = '[SourceDB]',
#TDBName VARCHAR(50) = '[TargertDB]',
#tableName VARCHAR(100) = ''
DECLARE #WhereF VARCHAR(max) ='',
#joincondition VARCHAR(max) ='',
#or VARCHAR(10) ='',
#select VARCHAR(max) = '',
#comma VARCHAR(1)='',
#query VARCHAR(max) ='',
#and VARCHAR(5)='',
#where1 VARCHAR(1000) ='',
#wOR VARCHAR(5)=''
SELECT #tableName = tablename
FROM #mdtables
WHERE id = #mdTableRowId;
SELECT #joincondition += Isnull(#and + ( CASE
WHEN cu.column_name IS NULL
THEN
NULL
ELSE ' src.[' + cu.column_name
+
'] = ' +
'trgt.['
+ c.column_name + ']'
END ), ''),
#WhereF += Isnull (#or + ( CASE
WHEN cu.column_name IS NOT NULL THEN
NULL
ELSE Isnull ( ' src.[' +
TC.column_name
+
'] ',
' isnull( src.[' +
C.column_name +
'],1) ' )
+ Isnull( '<> trgt.[' +
TC.column_name
+ ']',
' = isnull (src.['
+
C.column_name + '],1) ')
END ), ''),
#or = ( CASE
WHEN cu.column_name IS NOT NULL THEN ''
ELSE ' OR '
END ),
#and = ( CASE
WHEN cu.column_name IS NULL THEN ''
ELSE ' AND '
END ),
#select += #comma + ' src.[' + c.column_name + '] '
+ Isnull (' , trgt.[' + TC.column_name + ']', ''),
#comma = ',',
#where1 += Isnull(( #wOR + ( CASE
WHEN cu.column_name IS NULL THEN
NULL
ELSE ' trgt.[' + cu.column_name +
'] is null '
END ) ), ''),
#wOR = ( CASE
WHEN cu.column_name IS NULL THEN ''
ELSE ' OR '
END )
FROM information_schema.columns C
LEFT JOIN information_schema.key_column_usage CU
ON C.column_name = cu.column_name
AND constraint_name LIKE 'PK_%'
AND c.table_name = cu.table_name
LEFT JOIN [TargertDB].information_schema.columns TC
ON C.column_name = TC.column_name
AND c.table_name = TC.table_name
WHERE c.table_name = #tableName
--AND columnproperty(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') = 0
AND c.column_name NOT IN ( 'LST_CHG_TMS', 'LST_CHG_TMS',
'LST_CHG_USR_ID'
,
'LST_CHG_USR_ID' )
AND c.data_type NOT IN ( 'image' )
ORDER BY cu.column_name
SET #query = 'select ' + #select + ' from ' + #SDBName + '.dbo.'
+ #tableName + ' as src left join ' + #TDBName
+ '.dbo.' + #tableName + ' as trgt on '
+ #joincondition + ' where (' + #where1 + ')'
+ Isnull ('and '+ NULLIF (#WhereF, ''), '')
DECLARE #qu1 VARCHAR(max) =
' declare #cnt int =0 select #cnt =count (1) from '
+ #SDBName + '.dbo.' + #tableName
+ ' as src left join ' + #TDBName + '.dbo.'
+ #tableName + ' as trgt on ' + #joincondition
+ ' where (' + #where1 + ')'
+ Isnull (' OR '+ NULLIF (#WhereF, ''), '')
+ ' if (#cnt>0) begin select '''
+ #tableName + ''' as [ ],#cnt ' +-- #query + ' end '
BEGIN try
EXECUTE ( #qu1)
END try
BEGIN catch
PRINT #qu1;
END catch
SET #mdTableRowId = #mdTableRowId + 1
END
This might not need CTE's or sub-queries.
A few joins might do it.
SELECT
Usr1.ID AS db1_User_Id,
Usr1.First_name AS db1_First_name,
Usr1.Last_name AS db1_Last_name,
Usr1.Email AS db1_Email,
Usr1.Password AS db1_Password,
MAX(UsrReq1.Request_Id) AS db1_Request_Id,
MAX(UsrReq1.Request_name) AS db1_Request_name,
CASE WHEN COUNT(UsrReq2.User_Id) > 0 THEN 'Y' ELSE 'N' END AS Available_Db2,
CASE WHEN COUNT(UsrReq3.User_Id) > 0 THEN 'Y' ELSE 'N' END AS Available_Db3
FROM [Database1].[User] AS Usr1
LEFT JOIN [Database1].[User_request] AS UsrReq1 ON UsrReq1.User_Id = Usr1.ID
LEFT JOIN [Database2].[User] AS Usr2 ON Usr2.ID = Usr1.ID
LEFT JOIN [Database2].[User_request] AS UsrReq2 ON UsrReq2.User_Id = Usr2.ID
LEFT JOIN [Database3].[User] AS Usr3 ON Usr3.ID = Usr1.ID
LEFT JOIN [Database3].[User_request] AS UsrReq3 ON UsrReq3.User_Id = Usr3.ID
GROUP BY
Usr1.ID,
Usr1.First_name,
Usr1.Last_name,
Usr1.Email,
Usr1.Password;

Doesnt display the right count in dynamic sql

Here is the input table
I cannot get the right count of the sku. I need to achieve this by this dynamic sql. It gives result like below:
SET #SQL = 'SELECT DISTINCT
a.'+#primary_column+' AS [No. of distinct'' '+#primary_column+']
,sum(case when '+#secondary_column+' is null or '+#secondary_column+' = '''' then 0 else 1 end) AS ''Total_Count''
,STUFF((SELECT DISTINCT '', ['' +'+#secondary_column+'+'']''
FROM ['+#TableName+'] b
WHERE b.'+#primary_column+' = a.'+#primary_column+'
FOR XML PATH('''')),1, 1, '''') AS '+#secondary_column+'
FROM ['+#TableName+'] a
GROUP BY '+#primary_column+'';
PRINT(#SQL)
EXEC(#SQL)
This is the expected output
Change this:
,sum(case when '+#secondary_column+' is null or '+#secondary_column+' = '''' then 0 else 1 end) AS ''Total_Count''
On this:
,COUNT(DISTINCT NULLIF('+#secondary_column+'','''')) ''Total_Count''
Full query:
DECLARE #sql nvarchar(max),
#secondary_column nvarchar(max) = 'Parent_sku',
#primary_column nvarchar(max) = 'category',
#TableName nvarchar(max) = 'SomeTable'
SELECT #sql = '
SELECT ' + #primary_column + ' AS [No. of distinct'' ' + #primary_column+ '],
COUNT(DISTINCT ' + #secondary_column + ','''')) Total_count,
STUFF((SELECT DISTINCT '','' + QUOTENAME(ISNULL(' + #secondary_column + ',''''))
FROM ' + #TableName + ' s
WHERE s.' + #primary_column+ ' = a.' + #primary_column+ '
FOR XML PATH('''')),1,1,'''') as ' + #secondary_column + '
FROM ' + #TableName + ' a
GROUP BY ' + #primary_column
PRINT #sql
EXEC sp_executesql #sql
Output:
No. of distinct' category Total_count Parent_sku
1 [TB004]
Kids 1 [TB003]
Men 1 [TB001]
Women 1 [TB002]
EDIT
F.e. I got this as input:
('TB001L', 'TB001', 20, 'AQW2342S', 'Men'),
('TB001S', 'TB001', 21, 'GQW2342S', 'Men'),
('TB002M', 'TB002', 22, 'ZQW2342S', 'Women'),
('TB002S', 'TB002', 23, 'BQW2342S', 'Women'),
('TB003M', 'TB003', 20, 'KQW2342S', 'Kids'),
('TB003S', 'TB003', 23, 'RQW2342S', 'Kids'),
('TB005S', NULL, 22, 'RQW2342S', ''),
('TB005XL', NULL, 23, 'JQW2342S', '')
2 last rows is with NULL in #secondary_column. Output will be:
No. of distinct' category Total_count Parent_sku
0 []
Kids 1 [TB003]
Men 1 [TB001]
Women 1 [TB002]

PostgreSQL Create new table out of rows

I am using PostgreSQL 9.1
I have the following table containing some settings
setting | content
---------------+---------
enabled | True
reenable_time | 1
count_row | 6
count_col | 3
(4 rows)
And I want to have something like this
enabled | reenable_time | count_row | count_col
---------+-----------------+-------------+----------
True | 1 | 6 | 3
(1 row)
Thank you for your answers!
What you need is cross-tabulation (pivoting), aka turning rows into columns.
If you don't want to hardcode "enabled", "reenable_time", etc., you need to use dynamic SQL.
The basic idea goes like this:
SELECT -- Grab the user_account columns.
acc.id,
acc.email,
-- Use max() combined with a case statement to
-- usher individual attribute values into columns.
max(CASE WHEN (attr.attribute_type = 'first-name')
THEN attr.value END) AS fname,
max(CASE WHEN (attr.attribute_type = 'last-name')
THEN attr.value END) AS lname,
max(CASE WHEN (attr.attribute_type = 'title')
THEN attr.value END) AS title
FROM user_account AS acc
-- Join the attribute table /once/.
LEFT JOIN user_attribute AS attr
ON (attr.user_account_id = acc.id)
WHERE (acc.email = 'foo#example.com')
-- Group by the non-pivoted columns
GROUP BY acc.id,acc.email;
Here a link for further information:
https://sykosomatic.org/2011/09/pivot-tables-in-postgresql/
Here an example from my workplace (it's MS-SQL, but you'll get the idea, PostGre doesn't support the pivot command, it's MS proprietary, so you need to use "case when" with "group by"):
-- ===================================================
-- Author: Stefan Steiger
-- Create date: 14.04.2011
-- Last modified: 17.01.2012
-- Description: Übersetzung für Berichte
-- ===================================================
CREATE PROCEDURE [dbo].[sp_RPT_Report_Translation]
#in_mandant varchar(3)
,#in_sprache varchar(2)
,#in_stichtag varchar(50)
,#in_report_name nvarchar(1000)
AS
BEGIN
DECLARE
#strSQL NVARCHAR(MAX)
,#strReportName NVARCHAR(1000)
,#strPivotColumns NVARCHAR(MAX)
,#stichtag DATETIME
-- Abrunden des Eingabedatums auf 00:00:00 Uhr
SET #stichtag = CONVERT(DATETIME, #in_stichtag)
SET #stichtag = CAST(FLOOR(CAST(#stichtag AS Float)) AS DateTime)
SET #in_stichtag = CONVERT(varchar(50), #stichtag)
SET NOCOUNT ON;
SET #strReportName = REPLACE(#in_report_name, N'''', '''''')
-- http://geekswithblogs.net/baskibv/archive/2008/07/03/123567.aspx
SELECT
#strPivotColumns = COALESCE(#strPivotColumns, '') + '[' + [RTR_ItemCaption] + '], '
FROM T_RPT_Translations
WHERE (RTR_Status = 1)
AND (RTR_MDT_ID = #in_mandant)
AND
(
(RTR_ReportName = #strReportName)
OR
(RTR_ReportName = 'PARA_ALL')
)
--AND (RTR_ItemCaption != 'RPT_Title')
AND (RTR_ItemCaption IS NOT NULL)
AND
(
(RTR_IsFlag != 1)
OR
(RTR_IsFlag IS NULL)
)
AND (RTR_ItemCaption != '')
ORDER BY RTR_Sort
SET #strPivotColumns = SUBSTRING(#strPivotColumns, 0, LEN(#strPivotColumns))
SET #strPivotColumns = REPLACE(#strPivotColumns, N'''', '''''')
--PRINT #strPivotColumns
SET #strSQL = '
SELECT TOP(1) * FROM
(
SELECT
RTR_ItemCaption
--,RTR_Kurz_' + #in_sprache + '
,RTR_Lang_' + #in_sprache + '
FROM T_RPT_Translations
WHERE (RTR_MDT_ID = ''' + #in_mandant+ ''')
AND
(
(RTR_ReportName = ''' + #strReportName + ''')
OR
(RTR_ReportName = ''PARA_ALL'')
)
--AND (RTR_ItemCaption != ''RPT_Title'')
AND (RTR_Status = 1)
AND (RTR_ItemCaption IS NOT NULL)
AND
(
(RTR_IsFlag != 1)
OR
(RTR_IsFlag IS NULL)
)
AND (RTR_ItemCaption != '''')
) AS SourceTable
PIVOT
(
MAX(RTR_Lang_' + #in_sprache + ')
FOR RTR_ItemCaption IN
( '
+ #strPivotColumns +
' )
) AS PivotTable
--ORDER BY RPT_RM_SO_Bezeichnung, RPT_RM_GB_Bezeichnung, RPT_RM_NutzungGruppeCode
'
DECLARE #ProzedurParameter nvarchar(max)
SET #ProzedurParameter = '
DECLARE #in_mandant varchar(3)
,#in_sprache varchar(2)
,#in_stichtag varchar(50)
,#in_report_name nvarchar(1000)
;
SET #in_mandant = ''' + REPLACE(#in_mandant, '''', '''''') + ''';
SET #in_sprache = ''' + REPLACE(#in_sprache, '''', '''''') + ''';
SET #in_stichtag = ''' + REPLACE(#in_stichtag, '''', '''''') + ''';
SET #in_report_name = ''' + REPLACE(#in_report_name, '''', '''''') + ''';
'
EXECUTE sp_RPT_DEBUG_LOG_ProzedurRun
'sp_RPT_Report_Translation'
,#ProzedurParameter
,#strSQL
,'' --#ProzedurDetail
;
--PRINT #strSQL
EXECUTE (#strSQL)
END
GO