我有兩個數據表 DT1及DT2如何在存儲過程中傳遞的數據表
和我的存儲過程如下所示
ALTER procedure [dbo].[Sp_ShowAllEmpLeaveSummary]
@TableName1 nvarchar(128) ,
@TableName2 nvarchar(128)
as
begin
DECLARE @Columns VARCHAR(MAX)
SELECT @Columns = COALESCE(@Columns + ',' + name + '', '' + name + '') FROM (
SELECT DISTINCT
'CAST(ISNULL('[email protected] +'.' + name + ',0) AS VARCHAR) + ''/'' + CAST('[email protected] +'.' + name + ' AS VARCHAR) AS ' + name + ' ' AS name
FROM sys.columns
WHERE NAME LIKE '%leave%'
AND object_id IN (SELECT object_id FROM sys.tables WHERE name IN (''[email protected] +'', ''[email protected] +''))) LeaveColumns
DECLARE @SQL NVARCHAR(MAX)
SET @SQL = N'
SELECT
'[email protected] +'.empid,
'[email protected] +'.empname, ' + @Columns + '
FROM dt1
INNER JOIN '[email protected] +' ON '[email protected] +'.empid='[email protected] +'.empid'
EXECUTE(@SQL)
end
,並在UI我通過這樣的
cmd4.Parameters.Add(new SqlParameter("@TableName1", SqlDbType.Structured)).Value = dt1;
cmd4.Parameters.Add(new SqlParameter("@TableName2", SqlDbType.Structured)).Value = dt2;
參數
但顯示錯誤操作數類型衝突:表類型與nvarchar(128)不兼容。所以如果你想傳遞一個表我解決這個問題
感謝
將數據錶轉換爲XML字符串並將其傳遞給Procedure更好。 –