gpt4 book ai didi

mysql - 如何运行一组选择查询?

转载 作者:行者123 更新时间:2023-11-29 03:09:13 24 4
gpt4 key购买 nike

我有这个问题

SELECT 'GRANT SELECT ON ' + TABLE_NAME + ' TO '+ @UserName
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE' AND
TABLE_NAME LIKE 'aspnet%'

结果是这样的:

GRANT SELECT ON aspnet_Applications TO user04
GRANT SELECT ON aspnet_Membership TO user04
GRANT SELECT ON aspnet_Paths TO user04
GRANT SELECT ON aspnet_PersonalizationAllUsers TO user04
GRANT SELECT ON aspnet_PersonalizationPerUser TO user04
GRANT SELECT ON aspnet_Profile TO user04
GRANT SELECT ON aspnet_Roles TO user04
GRANT SELECT ON aspnet_SchemaVersions TO user04

我想在此结果中运行所有选择查询。我该怎么做?

最佳答案

DECLARE @Sql NVARCHAR(MAX)
SET @Sql = ''

SELECT @Sql = @Sql + 'GRANT SELECT ON ' + TABLE_NAME + ' TO '+ @UserName + '
' -- Newline to keep statements from being just one long line.
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE' AND
TABLE_NAME LIKE 'aspnet%'

-- Print it first to double-check that it looks right before running.
PRINT @Sql

-- After you've validated it with print, run it.
EXEC sp_executeSql @Sql

关于mysql - 如何运行一组选择查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10954179/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com