我有两个数据表 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('+@TableName1 +'.' + name + ',0) AS VARCHAR) + ''/'' + CAST('+@TableName2 +'.' + 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 (''+@TableName1 +'', ''+@TableName2 +''))) LeaveColumns
DECLARE @SQL NVARCHAR(MAX)
SET @SQL = N'
SELECT
'+@TableName1 +'.empid,
'+@TableName2 +'.empname, ' + @Columns + '
FROM dt1
INNER JOIN '+@TableName2 +' ON '+@TableName1 +'.empid='+@TableName2 +'.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) 不兼容。我能解决这个问题吗
谢谢