table-variable

TSQL Define Temp Table (or table variable) Without Defining Schema?

怎甘沉沦 提交于 2019-12-03 08:09:00
问题 Is there a way to define a temp table without defining it's schema up front? 回答1: Actually using a table VARIABLE, an in-memory table, is the optimal way to go. The #table creates a table in temp db, and ##table is global - both with disk hits. Consider the slow-down/hit experienced with the number of transactions. CREATE PROCEDURE [dbo].[GetAccounts] @AccountID BIGINT, @Result INT OUT, @ErrorMessage VARCHAR(255) OUT AS BEGIN SET NOCOUNT ON; SET @Result = 0 SET @ErrorMessage = '' DECLARE @tmp

How to use a record type variable in plpgsql?

天涯浪子 提交于 2019-12-03 05:21:53
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 count(*) FROM t INTO c; RAISE NOTICE '% results', c; SELECT * FROM t; END $$; ... but didn't work:

Table Valued Parameter: sending data in small chunks

隐身守侯 提交于 2019-12-02 22:27:43
问题 I am reading from a csv file and sending data as table variable to a stored procedure. From what i have tested so far , I am able to process 300k records in 3 mins 30 seconds . The file may contain up to millions of records as we go. I wanted to know if its a good idea to send all these records to the stored procedure in one go or Should I send them in batches of say 500k? I have set the command timeout to 1800. 回答1: An example of using IEnumerable SqlDataRecord It works kind of like a

Stored Procedure that has table argument in T-SQL

二次信任 提交于 2019-12-02 18:36:06
问题 Table Argument as OUTPUT I want to pass a table variable into a procedure that has table argument as output, but not as read only! I want to be able to modify that argument inside the PROC. Is this possible? If it's not possible, is there another way to do this? thanks! 回答1: You'd have to copy the table valued parameter into a table variable or temp table CREATE PROC DoStuff @tvp SomeTableType READONLY AS .. SELECT * INTO #LocalCopy FROM @tvp; -- take local copy ... DoStuff -- do processing

Table Valued Parameter: sending data in small chunks

帅比萌擦擦* 提交于 2019-12-02 13:43:00
I am reading from a csv file and sending data as table variable to a stored procedure. From what i have tested so far , I am able to process 300k records in 3 mins 30 seconds . The file may contain up to millions of records as we go. I wanted to know if its a good idea to send all these records to the stored procedure in one go or Should I send them in batches of say 500k? I have set the command timeout to 1800. An example of using IEnumerable SqlDataRecord It works kind of like a reverse datareader Notice I sort. This is by the clustered index. Fragmentation of the indexes will absolutely

Dynamic query results into a temp table or table variable

萝らか妹 提交于 2019-12-02 02:27:58
I have a stored procedure that uses sp_executesql to generate a result set, the number of columns in the result can vary but will be in the form of Col1 Col2 Col3 etc. I need to get the result into a temp table or table variable so I can work with it. The problem is I need to define the columns of the temp table, which I cant do dynamically using sp_executesql as the scope of the temp table is lost after the command is executed. I have toyed with the idea of using Global Temp tables, as the scope allows it to be created dynamically, however, there is a very good chance the Global Temps would

Dynamic query results into a temp table or table variable

我的梦境 提交于 2019-12-02 01:45:22
问题 I have a stored procedure that uses sp_executesql to generate a result set, the number of columns in the result can vary but will be in the form of Col1 Col2 Col3 etc. I need to get the result into a temp table or table variable so I can work with it. The problem is I need to define the columns of the temp table, which I cant do dynamically using sp_executesql as the scope of the temp table is lost after the command is executed. I have toyed with the idea of using Global Temp tables, as the

Query SQL Server with IN (NULL) not working

巧了我就是萌 提交于 2019-12-01 21:11:22
问题 When I define a "User-Defined Table Type", as: CREATE TYPE [dbo].[BitType] AS TABLE( [B] [bit] NULL ) I place 0 and null in this table-variable. Then I do this query: SELECT something FROM theTable WHERE item IN @theBitTypeTable Will only get item=0 not item is null Simply put: SELECT something FROM theTable WHERE item IN (0, NULL) is not working (no error although) It has to be SELECT something FROM theTable WHERE item=0 OR item IS NULL So, my question is, if I like to use User-Defined Table

Query SQL Server with IN (NULL) not working

徘徊边缘 提交于 2019-12-01 20:10:32
When I define a "User-Defined Table Type", as: CREATE TYPE [dbo].[BitType] AS TABLE( [B] [bit] NULL ) I place 0 and null in this table-variable. Then I do this query: SELECT something FROM theTable WHERE item IN @theBitTypeTable Will only get item=0 not item is null Simply put: SELECT something FROM theTable WHERE item IN (0, NULL) is not working (no error although) It has to be SELECT something FROM theTable WHERE item=0 OR item IS NULL So, my question is, if I like to use User-Defined Table Type , but I also need to use NULL value. How can I perform the query correctly to get result include

RODBC command 'sqlQuery' has problems with table variables in t-SQL

我的梦境 提交于 2019-12-01 06:28:41
I am using the RODBC package which I am applying on a Microsoft SQL Server 2012. Now I have discovered a phenomenon that puzzles me. If I run the following query with the RODBC command sqlQuery, then, in R, I will get back an empty data frame with the columns Country, CID, PriceID and WindID. DECLARE @tbl_IDs TABLE ( Country nvarchar(30), CID nvarchar(5), PriceID int, WindID int ) SELECT * FROM @tbl_Ids So far, everything is fine. However, if I try to write a record to the table variable and execute DECLARE @tbl_IDs TABLE ( Country nvarchar(30), CID nvarchar(5), PriceID int, WindID int )