Гость
Целевая тема:
Создать новую тему:
Автор:
Форумы / Microsoft SQL Server [игнор отключен] [закрыт для гостей] / Any advantages of using a cursor to retrieve just one record? / 4 сообщений из 4, страница 1 из 1
24.07.2002, 20:41:18
    #32038630
Gobzo Kobler
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Any advantages of using a cursor to retrieve just one record?
In the application I am maintaining there is a practice of storing the newly inserted identity in a special global temporary table for use from within the procs or scripts. The way they retrieve the stored value is this:

EXEC ('DECLARE id CURSOR FOR select id from ' + @identity_table_name + ' WHERE table_name = ' + "'" + @table_name + "'")

OPEN id
FETCH id INTO @some_id
DEALLOCATE id

Does not make sense for me. Why may they want to build up this stuff instead of a simple

EXEC ('SELECT @some_id = id from ' + @identity_table_name + ' WHERE table_name = ' + "'" + @table_name + "'")

??? In the rest of the code they strictly avoid using cursors and stick with SELECT TOP 1 instead.
...
Рейтинг: 0 / 0
24.07.2002, 20:55:28
    #32038633
Сергей Тихонов
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Any advantages of using a cursor to retrieve just one record?
А зачем вообще так делать?
Можно написать процедуру (таблица я так понимаю уже создана), которая на вход получает имя таблицы (или имя ключа), а в выходной параметр выдает значение ключа...

Нет никакого смысла здесь использовать курсоры. Курсоры очень тормозят работу сервера...
...
Рейтинг: 0 / 0
24.07.2002, 21:24:56
    #32038636
Glory
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Any advantages of using a cursor to retrieve just one record?
Maybe your developers want use identity value right after EXEC().

In this case you can use this script for return value from dynamic query

like this
Код: plaintext
1.
2.
3.
4.
declare @ret_id int, @mysql nvarchar( 4000 )
set @mysql = 'SELECT @some_id = id from ' + @identity_table_name + ' WHERE table_name = ' +  "'" + @table_name + "'" 

exec sp_executesql @mysql, N'@some_id int out', @some_id = @ret_id out


or like this
Код: plaintext
1.
2.
3.
4.
5.
6.
create table #t(ret_id int)

insert #t EXEC ('SELECT id from ' + @identity_table_name + ' WHERE table_name = ' +  "'" + @table_name + "'" ) 

SELECT @some_id = ret_id FROM #t
drop table #t
...
Рейтинг: 0 / 0
25.07.2002, 19:35:21
    #32038886
Gobzo Kobler
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Any advantages of using a cursor to retrieve just one record?
2All:

The reason for still using this practice is that it is a corporate standard. You all know that it is like a concrete wall... You can not break it, you can only drive along it.

The reason for storing @@identity in a ## table is that they have i, u & d triggers in each and every table and those triggers populate other tables with their own identities and they also call procs that do the same thing.

My concern is about the way they retrieve that one value from that table - why necessarily using a cursor where a simple select would do?

PS: Sorry for writing in English - I can't get ahold of w2k CD to install ru locale... And I hate translit.
...
Рейтинг: 0 / 0
Форумы / Microsoft SQL Server [игнор отключен] [закрыт для гостей] / Any advantages of using a cursor to retrieve just one record? / 4 сообщений из 4, страница 1 из 1
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


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