有没有办法在不使用动态 sql 查询的情况下动态控制 table 提示
Is there a way to dynamically control a table hint without using a dynamic sql query
例如:
CREATE PROCEDURE [dbo].[procGetTable]
(
@SetUPDLOCK BIT,
@RecordId BigInt
)
AS
SELECT *
FROM MYTABLE WITH (CASE WHEN @SetUPDLOCK = 1 THEN 'UPDLOCK' ELSE '' END)
WHERE MYTABLE.RecordId = @RecordId
/* P.S。我知道以上内容仅在概念上不起作用 */
CREATE PROCEDURE [dbo].[procGetTable]
@SetUPDLOCK BIT,
@RecordId BigInt
AS
BEGIN
SET NOCOUNT ON;
IF (@SetUPDLOCK = 1 )
BEGIN
SELECT *
FROM MYTABLE WITH (UPDLOCK)
WHERE MYTABLE.RecordId = @RecordId
END
ELSE
BEGIN
SELECT *
FROM MYTABLE
WHERE MYTABLE.RecordId = @RecordId
END
END
例如:
CREATE PROCEDURE [dbo].[procGetTable]
(
@SetUPDLOCK BIT,
@RecordId BigInt
)
AS
SELECT *
FROM MYTABLE WITH (CASE WHEN @SetUPDLOCK = 1 THEN 'UPDLOCK' ELSE '' END)
WHERE MYTABLE.RecordId = @RecordId
/* P.S。我知道以上内容仅在概念上不起作用 */
CREATE PROCEDURE [dbo].[procGetTable]
@SetUPDLOCK BIT,
@RecordId BigInt
AS
BEGIN
SET NOCOUNT ON;
IF (@SetUPDLOCK = 1 )
BEGIN
SELECT *
FROM MYTABLE WITH (UPDLOCK)
WHERE MYTABLE.RecordId = @RecordId
END
ELSE
BEGIN
SELECT *
FROM MYTABLE
WHERE MYTABLE.RecordId = @RecordId
END
END