powered by simpleCommunicator - 2.0.59     © 2026 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / Microsoft SQL Server [игнор отключен] [закрыт для гостей] / Вопрос по sp_executesql
3 сообщений из 3, страница 1 из 1
Вопрос по sp_executesql
    #32021286
Sasha
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Ребята, в чем глюк.
exec sp_executesql N'insert into table_name (column) values(value)'
Все нормально, а если так:

declare @tblname varchar(15)
set @tblname = 'table_name'
exec sp_executesql N'insert into '+ @tblname +' (column) values(value)'
или
declare @sQuery varchar(255)
set @sQuery = N'insert into tablse_name (column) values(value)'
exec sp_executesql @sQuery
ошибка. ???
С уважением, Саша.
...
Рейтинг: 0 / 0
Вопрос по sp_executesql
    #32021290
Glory
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
BOL - Transact-SQL Reference - System Stored Procedures - sp_executesql

"sp_executesql [@stmt =] stmt[{,[@params =] N'@parameter_name data_type [,...n]' }{,[@param1 =] 'value1' [,...n] }
]

Arguments
[@stmt =] stmt

Is a Unicode string containing a Transact-SQL statement or batch. stmt must be either a Unicode constant or a variable that can be implicitly converted to ntext. More complex Unicode expressions (such as concatenating two strings with the + operator) are not allowed. Character constants are not allowed. If a constant is specified, it must be prefixed with an N. For example, the Unicode constant N'sp_who' is legal, but the character constant 'sp_who' is not. The size of the string is limited only by available database server memory"

Надо так
declare @sQuery nvarchar(255)
set @sQuery = N'insert into tablse_name (column) values(value)'
exec sp_executesql @sQuery
...
Рейтинг: 0 / 0
Вопрос по sp_executesql
    #32021374
Фотография ASCRUS
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
А еще можно так:

exec('insert into '+ @tblname +' (column) values(value)')
...
Рейтинг: 0 / 0
3 сообщений из 3, страница 1 из 1
Форумы / Microsoft SQL Server [игнор отключен] [закрыт для гостей] / Вопрос по sp_executesql
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]