是的,但僅當使用動態SQL(存儲過程)時。 在這裏,在現實生活中使用
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_RPT_Report_Translation]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sp_RPT_Report_Translation]
GO
-- ===========================================================
-- Author: [CENSORED]
-- Create date: 14.04.2011
-- Last modified: 17.01.2012
-- Description: Übersetzung für Berichte
-- ===========================================================
-- Pre: Valid Report Name & datetime
-- Post: Translation for language in first row with rownames as defined
-- for each item in T_RPT_Translations.RTR_ItemCaption
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
,'' [email protected]
;
--PRINT @strSQL
EXECUTE (@strSQL)
END
GO
此表的例子:
CREATE TABLE [dbo].[T_RPT_Translations](
[RTR_UID] [uniqueidentifier] NULL,
[RTR_ReportName] [nvarchar](1000) NULL,
[RTR_MDT_ID] [int] NULL,
[RTR_ItemCaption] [nvarchar](50) NULL,
[RTR_Code] [int] NULL,
[RTR_nCode] [nvarchar](100) NULL,
[RTR_Kurz_DE] [nvarchar](20) NULL,
[RTR_Kurz_FR] [nvarchar](20) NULL,
[RTR_Kurz_IT] [nvarchar](20) NULL,
[RTR_Kurz_EN] [nvarchar](20) NULL,
[RTR_Lang_DE] [nvarchar](100) NULL,
[RTR_Lang_FR] [nvarchar](100) NULL,
[RTR_Lang_IT] [nvarchar](100) NULL,
[RTR_Lang_EN] [nvarchar](100) NULL,
[RTR_Img_DE] [varchar](max) NULL,
[RTR_Img_FR] [varchar](max) NULL,
[RTR_Img_IT] [varchar](max) NULL,
[RTR_Img_EN] [varchar](max) NULL,
[RTR_Img_Width] [int] NULL,
[RTR_Img_Height] [int] NULL,
[RTR_Img_PaddingLeft] [float] NULL,
[RTR_Img_PaddingRight] [float] NULL,
[RTR_Img_PaddingTop] [float] NULL,
[RTR_Img_PaddingBottom] [float] NULL,
[RTR_Img_Hide] [bit] NULL,
[RTR_IsLogo] [bit] NULL,
[RTR_IsFlag] [bit] NULL,
[RTR_Sort] [int] NULL,
[RTR_Status] [int] NULL,
[RTR_DatumVon] [datetime] NULL,
[RTR_DatumBis] [datetime] NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[T_RPT_Translations] ADD CONSTRAINT [DF_T_RPT_Translations_RTR_UID] DEFAULT (newid()) FOR [RTR_UID]
GO
ALTER TABLE [dbo].[T_RPT_Translations] ADD CONSTRAINT [DF_T_RPT_Translations_RTR_ReportName] DEFAULT (N'InsertError') FOR [RTR_ReportName]
GO
ALTER TABLE [dbo].[T_RPT_Translations] ADD CONSTRAINT [DF_T_RPT_Translations_RTR_MDT_ID] DEFAULT ((0)) FOR [RTR_MDT_ID]
GO
ALTER TABLE [dbo].[T_RPT_Translations] ADD CONSTRAINT [DF_T_RPT_Translations_RTR_ItemCaption] DEFAULT (N'InsertError') FOR [RTR_ItemCaption]
GO
ALTER TABLE [dbo].[T_RPT_Translations] ADD CONSTRAINT [DF_T_RPT_Translations_RTR_IsLogo] DEFAULT ((0)) FOR [RTR_IsLogo]
GO
ALTER TABLE [dbo].[T_RPT_Translations] ADD CONSTRAINT [DF_T_RPT_Translations_RTR_IsFlag] DEFAULT ((0)) FOR [RTR_IsFlag]
GO
ALTER TABLE [dbo].[T_RPT_Translations] ADD CONSTRAINT [DF_T_RPT_Translations_RTR_Sort] DEFAULT ((0)) FOR [RTR_Sort]
GO
ALTER TABLE [dbo].[T_RPT_Translations] ADD CONSTRAINT [DF_T_RPT_Translations_RTR_Status] DEFAULT ((1)) FOR [RTR_Status]
GO
ALTER TABLE [dbo].[T_RPT_Translations] ADD CONSTRAINT [DF_T_RPT_Translations_RTR_DatumVon] DEFAULT ('17530101') FOR [RTR_DatumVon]
GO
ALTER TABLE [dbo].[T_RPT_Translations] ADD CONSTRAINT [DF_T_RPT_Translations_RTR_DatumBis] DEFAULT ('99991231') FOR [RTR_DatumBis]
GO
如果你不理解的參數,這是在德國:
@in_sprache: in_language
@in_stichtag: in_ReportingDate
DatumVon: DateFrom
DatumBis: DateTo
rest is clear
RTR_Img_XX是一個base64編碼的圖像,在RTR_nCode中定義了mime,以防萬一。
MS-SQL使用T-SQL,而不是PL/SQL。除此之外,一個不可移植的存儲過程可以針對目標系統進行重寫 - 但無疑經常需要考慮。而複雜的查詢幾乎不可移植。 – 2012-02-08 14:51:29
我跟着上面的數字2使用代碼來建立選擇使用您提供的相同的語法,謝謝。 – bejumi 2012-02-09 09:22:33
@bejumi:這是個好主意。 – 2012-02-09 12:12:42