N'#39;''执行存储过程时默认添加前缀

时间:2018-01-13 21:18:59

标签: sql sql-server stored-procedures sql-server-2014

执行存储过程时,我传递了4个参数。通过GUI模式传递参数后,我发现sql本身已经添加了一些前缀。

USE [du_Attendance]
GO

DECLARE @return_value Int

EXEC    @return_value = [dbo].[GET_ATTENDANCE_REPORT_FOR_FACULTY]
        @startdate = '1/9/2018',
        @enddate = '1/12/2018',
        @coursecode = N'''BSCCS''',
        @subjectcode = N'''CSHT101'''

SELECT  @return_value as 'Return Value'

GO

因此,当我删除N'时,它会给我一个错误,指出无效的列名称&BSCCS'。这是什么意思?

完整存储过程 -

CREATE PROCEDURE GET_ATTENDANCE_REPORT_FOR_FACULTY
@startdate DATE,  
@enddate DATE,
@coursecode varchar(10),
@subjectcode varchar(10)


AS BEGIN

DECLARE @cols as varchar(2000);
DECLARE @cols_select as varchar(MAX);
declare @col varchar(20)
declare @cols_copy varchar(100)
DECLARE @query as varchar(MAX);

WITH cte (startdate)
AS 
(SELECT
        @startdate AS startdate
    UNION ALL
    SELECT
        DATEADD(DD, 1, startdate) AS startdate
    FROM cte
    WHERE startdate < @enddate
)
select c.startdate
into #tempDates
from cte c
where datename(weekday, c.startdate) <> 'Sunday';

SELECT
    @cols = STUFF((SELECT DISTINCT ',' + QUOTENAME(CONVERT(CHAR(6),startdate, 106))
        FROM #tempDates
        FOR XML PATH (''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')

select @cols_copy = replace(@cols, ',', '')

select @cols_select = SUBSTRING(@cols_select, 2, len(@cols_select) - 1)


SET @query = 'SELECT RollNo,FirstName,LastName, ' + dbo.fn_convert_cols(@cols) + ' from 
             (
                select S.RollNo,U.FirstName,U.LastName,
                D.startdate,
                convert(CHAR(6), startdate, 106) PivotDate
                from #tempDates D,Attendance A, Student S, UserDetails U
                where convert(CHAR(6), D.startdate, 106) = convert(CHAR(6), A.Date, 106) and A.EnrollmentNo=S.EnrollmentNo and A.EnrollmentNo=U.userID and A.CourseCode=' + @coursecode + ' and A.SubjectCode =' + @subjectcode + '
            ) x
           pivot 
           (
                count(startdate)
                for PivotDate in (' + @cols + ')
           ) p '

EXECUTE (@query)
drop table #tempDates
END

这个问题是我从C#代码中传递了参数,所以只有&#39; BSCCS&#39;传递到存储过程导致上述错误。

C#代码 -

SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(Constants.getAttendanceForFaculty, con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@startdate", startdate);
cmd.Parameters.AddWithValue("@enddate", enddate);
cmd.Parameters.AddWithValue("@coursecode", courseCode);
cmd.Parameters.AddWithValue("@subjectcode", subCode);
DataTable dt = new DataTable();
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
con.Close();

1 个答案:

答案 0 :(得分:1)

你应该改变你的动态查询,否则 - 因为动态查询 - SQL会像列一样执行你的参数。

我改变了

A.CourseCode=' + @coursecode + ' and A.SubjectCode =' + @subjectcode + '

到这个

A.CourseCode=''' + @coursecode + ''' and A.SubjectCode =''' + @subjectcode + '''

SET @query = 'SELECT RollNo,FirstName,LastName, ' + dbo.fn_convert_cols(@cols) + ' from 
             (
                select S.RollNo,U.FirstName,U.LastName,
                D.startdate,
                convert(CHAR(6), startdate, 106) PivotDate
                from #tempDates D,Attendance A, Student S, UserDetails U
                where convert(CHAR(6), D.startdate, 106) = convert(CHAR(6), A.Date, 106) and A.EnrollmentNo=S.EnrollmentNo and A.EnrollmentNo=U.userID and A.CourseCode=''' + @coursecode + ''' and A.SubjectCode =''' + @subjectcode + '''
            ) x
           pivot 
           (
                count(startdate)
                for PivotDate in (' + @cols + ')
           ) p '