Some notes:
ExecuteNonQuery
returns -1ExecuteNonQuery
will drop the table (@droptable
), but it will not create the new table (@code
)- the length of the
@code
query is 10265 characters - The stored procedure runs perfectly fine in SSMS and returns 22 rows in the table
Are there any ideas as to why C#'s ExecuteNonQuery
function doesn't seem to be executing the 'exec(@code)' portion of the stored procedure?
ALTER procedure [dbo].[sp_create_EditControlResultsPivot]
as
begin
declare @t nvarchar (250);
set @t = 'editControlResults'
declare @newtable nvarchar(250);
set @newtable = 'dbo.' + @t + 'Pivot'
declare @nonPivotColumn1 nvarchar(250);
set @nonPivotColumn1 = 'num'
declare @nonPivotColumn2 nvarchar(25);
set @nonPivotColumn2 = 'File_Name'
declare @droptable nvarchar(max);
set @droptable =
'if EXISTS (select * from sys.objects where object_id = object_id(N''' + @newtable + '''))
begin drop table ' + @newtable + ' end
'
declare @i int
set @i = 1;
declare @itemList nvarchar(max);
declare @code nvarchar(max);
while @i <= (
select COUNT(*)
from sys.columns c
left join sys.tables t on c.object_id = t.object_id
where 1=1
and c.name not like @nonPivotColumn1
and c.name not like @nonPivotColumn2
and t.name = @t
)
begin
set @itemList = @itemList + ', ' +
(
select col from
(
select c.name as col, ROW_NUMBER () over (order by c.name) as num from
sys.columns c left join sys.tables t on c.object_id = t.object_id
where 1=1
and c.name not like @nonPivotColumn1
and c.name not like @nonPivotColumn2
and t.name = @t
) sub where num = @i
)
set @i = @i + 1
end
set @itemList = (select substring(@itemList, 2, LEN(@itemList)))
set @code = '
SELECT ' + @nonpivotcolumn2 + ', Item
into ' + @newtable + '
FROM
(SELECT ' + @nonpivotcolumn2 + ', ' + @itemList + '
FROM ' + @t + ') sub
UNPIVOT
(Value FOR Item IN (' + @itemList + ')
) AS sub
where Value = ''true''
'
exec(@droptable)
exec(@code);
--print(len(@code))
END
--exec sp_create_EditControlResultsPivot
The ExecuteNonQuery Method returns the number of rows affected use the ExecuteReader method instead.
SqlCommand.ExecuteReader Method
The only way to return data from ExecuteNonQuery would be via an Output parameter.