table-variable

How to use table variable in a dynamic sql statement?

。_饼干妹妹 提交于 2019-12-17 02:24:14
问题 In my stored procedure I declared two table variables on top of my procedure. Now I am trying to use that table variable within a dynamic sql statement but I get this error at the time of execution of that procedure. I am using Sql Server 2008. This is how my query looks like, set @col_name = 'Assoc_Item_' + Convert(nvarchar(2), @curr_row1); set @sqlstat = 'update @RelPro set ' + @col_name + ' = (Select relsku From @TSku Where tid = ' + Convert(nvarchar(2), @curr_row1) + ') Where RowID = ' +

How to use a table variable in an “update from select” query?

依然范特西╮ 提交于 2019-12-10 15:39:54
问题 I have this table variable declaration followed by a query: DECLARE @CurrentItems TABLE ( ItemId uniqueidentifier, ItemUnits int ) UPDATE U SET U.Units = U.Units + [@CurrentItems].ItemUnits FROM @CurrentItems CI INNER JOIN U ON U.UId=CI.ItemId; And U is defined as follows: CREATE TABLE [dbo].[U] ( [UId] UNIQUEIDENTIFIER UNIQUE NOT NULL, [Units] INT DEFAULT ((0)) NOT NULL ); When I run that in SQL Management Studio against SQL Server 2005 Express I get the following: Msg 208, Level 16, State 1

Can queries that read table variables generate parallel exection plans in SQL Server 2008?

烈酒焚心 提交于 2019-12-10 15:34:13
问题 First, from BOL: Queries that modify table variables do not generate parallel query execution plans. Performance can be affected when very large table variables, or table variables in complex queries, are modified. In these situations, consider using temporary tables instead. For more information, see CREATE TABLE (Transact-SQL). Queries that read table variables without modifying them can still be parallelized. That seems clear enough. Queries that read table variables, without modifying

table variables created and held in memory or in tempdb?

◇◆丶佛笑我妖孽 提交于 2019-12-09 15:01:35
问题 Are table variables created in memory or in tempdb? Same for short temp tables? 回答1: A temp table will be created in tempdb and you can easily check for it by querying the sysobjects table in tempdb example create table #test (Item char(1), TimeSold varchar(20)) select * from tempdb.sys.sysobjects where name like '#test%' you should see something with a name like #test_______000000000905 but then with more underscores If you need to check if a temp table exists then see also How Do You Check

Table variables inside while loop not initializing everytime : SQL Server

风格不统一 提交于 2019-12-07 04:10:20
问题 I am wondering why the table variables inside while loop does not behave like other variables. Table variables created only once and will be used across through out whole looping. but other variables getting initialized every time when loop increases. Check out the below code for more info declare @tt int set @tt =10 while @tt>0 begin declare @temptable table(id int identity(1,1),sid bigint) insert into @temptable select @tt union all select @tt + 1 select * from @temptable --delete from

Table variables inside while loop not initializing everytime : SQL Server

空扰寡人 提交于 2019-12-05 10:15:48
I am wondering why the table variables inside while loop does not behave like other variables. Table variables created only once and will be used across through out whole looping. but other variables getting initialized every time when loop increases. Check out the below code for more info declare @tt int set @tt =10 while @tt>0 begin declare @temptable table(id int identity(1,1),sid bigint) insert into @temptable select @tt union all select @tt + 1 select * from @temptable --delete from @temptable set @tt=@tt-1 end is this a bug?? Your premise is wrong. Other variables don't get reinitialised

Get definitive names for columns from table variable

若如初见. 提交于 2019-12-05 00:11:42
问题 I can declare a table variable as such: DECLARE @tv_source TABLE(c1 int, providerName varchar(50), providerSMS varchar(50)) If I then execute the following, I see the table name similar to: "#468862B0" select top 1 * from tempdb.sys.tables where type = 'U' order by create_date desc select TOP 1 name,* from tempdb.sys.sysobjects ORDER BY CRDATE desc If I then immediately execute: select TOP 3 * from tempdb.sys.columns where object_id in (select TOP 1 object_id from tempdb.sys.tables ORDER BY

How to use a record type variable in plpgsql?

こ雲淡風輕ζ 提交于 2019-12-04 10:10:34
问题 How can I use query result stored into a record type variable for another query within the same stored function? I use Postgres 9.4.4. With a table like this: create table test (id int, tags text[]); insert into test values (1,'{a,b,c}'), (2,'{c,d,e}'); I wrote a function (simplified) like below: CREATE OR REPLACE FUNCTION func(_tbl regclass) RETURNS TABLE (t TEXT[], e TEXT[]) LANGUAGE plpgsql AS $$ DECLARE t RECORD; c INT; BEGIN EXECUTE format('SELECT id, tags FROM %s', _tbl) INTO t; SELECT

table variables created and held in memory or in tempdb?

醉酒当歌 提交于 2019-12-04 01:15:51
Are table variables created in memory or in tempdb? Same for short temp tables? A temp table will be created in tempdb and you can easily check for it by querying the sysobjects table in tempdb example create table #test (Item char(1), TimeSold varchar(20)) select * from tempdb.sys.sysobjects where name like '#test%' you should see something with a name like #test_______000000000905 but then with more underscores If you need to check if a temp table exists then see also How Do You Check If A Temporary Table Exists In SQL Server The structure of Table variable is also created in tempdb To see

Get definitive names for columns from table variable

妖精的绣舞 提交于 2019-12-03 16:04:24
I can declare a table variable as such: DECLARE @tv_source TABLE(c1 int, providerName varchar(50), providerSMS varchar(50)) If I then execute the following, I see the table name similar to: "#468862B0" select top 1 * from tempdb.sys.tables where type = 'U' order by create_date desc select TOP 1 name,* from tempdb.sys.sysobjects ORDER BY CRDATE desc If I then immediately execute: select TOP 3 * from tempdb.sys.columns where object_id in (select TOP 1 object_id from tempdb.sys.tables ORDER BY Create_date desc) I see the columns I declared above for the table variable. My question is, is there