如何阻止 Visual Studio 查询生成器重写 SQL (VS2005)
How to stop Visual Studio Query Builder from re-writing SQL (VS2005)
我只是粘贴我在 SQL Server Management Studio 中精心制作的这段 SQL 代码,它可以正常工作。
SELECT v.StudentID, v.StudentName, s.StudentHomeEmailAddress, s.StudentStudyYear, s.StudentMobilePhone, t.TradeName, p.ProgramName, (CASE WHEN s.StudentHomeEmailAddress IS NULL THEN 1 ELSE 0 END) AS Ord1
FROM vwCurrentStudents AS v
INNER JOIN tblStudents AS s ON s.StudentID = v.StudentID
INNER JOIN tblStudentProgramReg AS r ON r.StudentID = s.StudentID AND r.StudProgEnrolStatusID IN (1, 3)
INNER JOIN tblPrograms AS p ON r.ProgramID = p.ProgramID AND p.ProgramCatID IN (1, 3) AND p.ProgramID NOT IN (23, 112, 113)
INNER JOIN tblTrades AS t ON t.TradeID = p.TradeID
WHERE (@Who = 'Stu')
AND (s.StudentStudyYear LIKE @StudyYear)
AND (
(CASE WHEN (SELECT Item FROM dbo.fnMakeTableFromList(@Programs, ',') AS mkTblP WHERE (LTRIM(RTRIM(UPPER(Item))) = LTRIM(RTRIM(UPPER(p.ProgramName))))) = p.ProgramName THEN 1 ELSE 0 END = 1 OR dbo.fnISEMPTY(@Programs) = 1 OR @Programs = '%')
AND (CASE WHEN (SELECT Item FROM dbo.fnMakeTableFromList(@Trades, ',') AS mkTblT WHERE (LTRIM(RTRIM(UPPER(Item))) = LTRIM(RTRIM(UPPER(t.TradeName))))) = t.TradeName THEN 1 ELSE 0 END = 1 OR dbo.fnISEMPTY(@Trades) = 1 OR @Trades = '%')
)
ORDER BY Ord1, v.StudentName
但是,如果我将它粘贴到查询生成器中,它会尝试生成 table 链接等...查询将更改为这个!
SELECT v.StudentID, v.StudentName, s.StudentHomeEmailAddress, s.StudentStudyYear, s.StudentMobilePhone, t.TradeName, p.ProgramName,
(CASE WHEN s.StudentHomeEmailAddress IS NULL THEN 1 ELSE 0 END) AS Ord1
FROM vwCurrentStudents AS v INNER JOIN
tblStudents AS s ON s.StudentID = v.StudentID INNER JOIN
tblStudentProgramReg AS r ON r.StudentID = s.StudentID AND r.StudProgEnrolStatusID IN (1, 3) INNER JOIN
tblPrograms AS p ON r.ProgramID = p.ProgramID AND p.ProgramCatID IN (1, 3) AND p.ProgramID NOT IN (23, 112, 113) INNER JOIN
tblTrades AS t ON t.TradeID = p.TradeID
WHERE (@Who = 'Stu') AND (s.StudentStudyYear LIKE @StudyYear) AND (CASE WHEN
(SELECT Item
FROM dbo.fnMakeTableFromList(@Programs, ',') AS mkTblP
WHERE (LTRIM(RTRIM(UPPER(Item))) = LTRIM(RTRIM(UPPER(p.ProgramName))))) = p.ProgramName THEN 1 ELSE 0 END = 1) AND
(CASE WHEN
(SELECT Item
FROM dbo.fnMakeTableFromList(@Trades, ',') AS mkTblT
WHERE (LTRIM(RTRIM(UPPER(Item))) = LTRIM(RTRIM(UPPER(t .TradeName))))) = t .TradeName THEN 1 ELSE 0 END = 1) OR
(@Who = 'Stu') AND (s.StudentStudyYear LIKE @StudyYear) AND (CASE WHEN
(SELECT Item
FROM dbo.fnMakeTableFromList(@Trades, ',') AS mkTblT
WHERE (LTRIM(RTRIM(UPPER(Item))) = LTRIM(RTRIM(UPPER(t .TradeName))))) = t .TradeName THEN 1 ELSE 0 END = 1) AND
(dbo.fnISEMPTY(@Programs) = 1) OR
(@Who = 'Stu') AND (s.StudentStudyYear LIKE @StudyYear) AND (CASE WHEN
(SELECT Item
FROM dbo.fnMakeTableFromList(@Trades, ',') AS mkTblT
WHERE (LTRIM(RTRIM(UPPER(Item))) = LTRIM(RTRIM(UPPER(t .TradeName))))) = t .TradeName THEN 1 ELSE 0 END = 1) AND
(@Programs = '%') OR
(@Who = 'Stu') AND (s.StudentStudyYear LIKE @StudyYear) AND (CASE WHEN
(SELECT Item
FROM dbo.fnMakeTableFromList(@Programs, ',') AS mkTblP
WHERE (LTRIM(RTRIM(UPPER(Item))) = LTRIM(RTRIM(UPPER(p.ProgramName))))) = p.ProgramName THEN 1 ELSE 0 END = 1) AND
(dbo.fnISEMPTY(@Trades) = 1) OR
(@Who = 'Stu') AND (s.StudentStudyYear LIKE @StudyYear) AND (dbo.fnISEMPTY(@Programs) = 1) AND (dbo.fnISEMPTY(@Trades) = 1) OR
(@Who = 'Stu') AND (s.StudentStudyYear LIKE @StudyYear) AND (@Programs = '%') AND (dbo.fnISEMPTY(@Trades) = 1) OR
(@Who = 'Stu') AND (s.StudentStudyYear LIKE @StudyYear) AND (CASE WHEN
(SELECT Item
FROM dbo.fnMakeTableFromList(@Programs, ',') AS mkTblP
WHERE (LTRIM(RTRIM(UPPER(Item))) = LTRIM(RTRIM(UPPER(p.ProgramName))))) = p.ProgramName THEN 1 ELSE 0 END = 1) AND
(@Trades = '%') OR
(@Who = 'Stu') AND (s.StudentStudyYear LIKE @StudyYear) AND (dbo.fnISEMPTY(@Programs) = 1) AND (@Trades = '%') OR
(@Who = 'Stu') AND (s.StudentStudyYear LIKE @StudyYear) AND (@Programs = '%') AND (@Trades = '%')
ORDER BY Ord1, v.StudentName
而且它不起作用。这是错的。 ORs
和 ANDs
的位置是错误的。
有什么办法可以阻止它吗?
我唯一的其他选择是直接将它粘贴到 ASP 代码视图中并仔细确保它 SQL 兼容,因为它不喜欢换行符、& 符号等
我通过关闭查询生成器的 table 布局的显示解决了这个问题。代码保持您粘贴它的方式。感谢@Ken 的提示,尽管它是讽刺的。
我只是粘贴我在 SQL Server Management Studio 中精心制作的这段 SQL 代码,它可以正常工作。
SELECT v.StudentID, v.StudentName, s.StudentHomeEmailAddress, s.StudentStudyYear, s.StudentMobilePhone, t.TradeName, p.ProgramName, (CASE WHEN s.StudentHomeEmailAddress IS NULL THEN 1 ELSE 0 END) AS Ord1
FROM vwCurrentStudents AS v
INNER JOIN tblStudents AS s ON s.StudentID = v.StudentID
INNER JOIN tblStudentProgramReg AS r ON r.StudentID = s.StudentID AND r.StudProgEnrolStatusID IN (1, 3)
INNER JOIN tblPrograms AS p ON r.ProgramID = p.ProgramID AND p.ProgramCatID IN (1, 3) AND p.ProgramID NOT IN (23, 112, 113)
INNER JOIN tblTrades AS t ON t.TradeID = p.TradeID
WHERE (@Who = 'Stu')
AND (s.StudentStudyYear LIKE @StudyYear)
AND (
(CASE WHEN (SELECT Item FROM dbo.fnMakeTableFromList(@Programs, ',') AS mkTblP WHERE (LTRIM(RTRIM(UPPER(Item))) = LTRIM(RTRIM(UPPER(p.ProgramName))))) = p.ProgramName THEN 1 ELSE 0 END = 1 OR dbo.fnISEMPTY(@Programs) = 1 OR @Programs = '%')
AND (CASE WHEN (SELECT Item FROM dbo.fnMakeTableFromList(@Trades, ',') AS mkTblT WHERE (LTRIM(RTRIM(UPPER(Item))) = LTRIM(RTRIM(UPPER(t.TradeName))))) = t.TradeName THEN 1 ELSE 0 END = 1 OR dbo.fnISEMPTY(@Trades) = 1 OR @Trades = '%')
)
ORDER BY Ord1, v.StudentName
但是,如果我将它粘贴到查询生成器中,它会尝试生成 table 链接等...查询将更改为这个!
SELECT v.StudentID, v.StudentName, s.StudentHomeEmailAddress, s.StudentStudyYear, s.StudentMobilePhone, t.TradeName, p.ProgramName,
(CASE WHEN s.StudentHomeEmailAddress IS NULL THEN 1 ELSE 0 END) AS Ord1
FROM vwCurrentStudents AS v INNER JOIN
tblStudents AS s ON s.StudentID = v.StudentID INNER JOIN
tblStudentProgramReg AS r ON r.StudentID = s.StudentID AND r.StudProgEnrolStatusID IN (1, 3) INNER JOIN
tblPrograms AS p ON r.ProgramID = p.ProgramID AND p.ProgramCatID IN (1, 3) AND p.ProgramID NOT IN (23, 112, 113) INNER JOIN
tblTrades AS t ON t.TradeID = p.TradeID
WHERE (@Who = 'Stu') AND (s.StudentStudyYear LIKE @StudyYear) AND (CASE WHEN
(SELECT Item
FROM dbo.fnMakeTableFromList(@Programs, ',') AS mkTblP
WHERE (LTRIM(RTRIM(UPPER(Item))) = LTRIM(RTRIM(UPPER(p.ProgramName))))) = p.ProgramName THEN 1 ELSE 0 END = 1) AND
(CASE WHEN
(SELECT Item
FROM dbo.fnMakeTableFromList(@Trades, ',') AS mkTblT
WHERE (LTRIM(RTRIM(UPPER(Item))) = LTRIM(RTRIM(UPPER(t .TradeName))))) = t .TradeName THEN 1 ELSE 0 END = 1) OR
(@Who = 'Stu') AND (s.StudentStudyYear LIKE @StudyYear) AND (CASE WHEN
(SELECT Item
FROM dbo.fnMakeTableFromList(@Trades, ',') AS mkTblT
WHERE (LTRIM(RTRIM(UPPER(Item))) = LTRIM(RTRIM(UPPER(t .TradeName))))) = t .TradeName THEN 1 ELSE 0 END = 1) AND
(dbo.fnISEMPTY(@Programs) = 1) OR
(@Who = 'Stu') AND (s.StudentStudyYear LIKE @StudyYear) AND (CASE WHEN
(SELECT Item
FROM dbo.fnMakeTableFromList(@Trades, ',') AS mkTblT
WHERE (LTRIM(RTRIM(UPPER(Item))) = LTRIM(RTRIM(UPPER(t .TradeName))))) = t .TradeName THEN 1 ELSE 0 END = 1) AND
(@Programs = '%') OR
(@Who = 'Stu') AND (s.StudentStudyYear LIKE @StudyYear) AND (CASE WHEN
(SELECT Item
FROM dbo.fnMakeTableFromList(@Programs, ',') AS mkTblP
WHERE (LTRIM(RTRIM(UPPER(Item))) = LTRIM(RTRIM(UPPER(p.ProgramName))))) = p.ProgramName THEN 1 ELSE 0 END = 1) AND
(dbo.fnISEMPTY(@Trades) = 1) OR
(@Who = 'Stu') AND (s.StudentStudyYear LIKE @StudyYear) AND (dbo.fnISEMPTY(@Programs) = 1) AND (dbo.fnISEMPTY(@Trades) = 1) OR
(@Who = 'Stu') AND (s.StudentStudyYear LIKE @StudyYear) AND (@Programs = '%') AND (dbo.fnISEMPTY(@Trades) = 1) OR
(@Who = 'Stu') AND (s.StudentStudyYear LIKE @StudyYear) AND (CASE WHEN
(SELECT Item
FROM dbo.fnMakeTableFromList(@Programs, ',') AS mkTblP
WHERE (LTRIM(RTRIM(UPPER(Item))) = LTRIM(RTRIM(UPPER(p.ProgramName))))) = p.ProgramName THEN 1 ELSE 0 END = 1) AND
(@Trades = '%') OR
(@Who = 'Stu') AND (s.StudentStudyYear LIKE @StudyYear) AND (dbo.fnISEMPTY(@Programs) = 1) AND (@Trades = '%') OR
(@Who = 'Stu') AND (s.StudentStudyYear LIKE @StudyYear) AND (@Programs = '%') AND (@Trades = '%')
ORDER BY Ord1, v.StudentName
而且它不起作用。这是错的。 ORs
和 ANDs
的位置是错误的。
有什么办法可以阻止它吗?
我唯一的其他选择是直接将它粘贴到 ASP 代码视图中并仔细确保它 SQL 兼容,因为它不喜欢换行符、& 符号等
我通过关闭查询生成器的 table 布局的显示解决了这个问题。代码保持您粘贴它的方式。感谢@Ken 的提示,尽管它是讽刺的。