table-variable

How to take table name as an input parameter to the stored procedure?

梦想的初衷 提交于 2019-11-30 08:22:14
问题 I have a small stored procedure below. I am taking the table name as an input parameter to the stored procedure so that I'm planning to insert the data into the temp table and display the same. This is just a tiny code block of my project stored procedure. When I am compiling the below, it is considering the parameter in the select statement as a table variable and throwing the error as: Must declare the table variable "@TableName". SQL: CREATE PROCEDURE xyz @TableName Varchar(50) AS BEGIN

How do I drop table variables in SQL-Server? Should I even do this?

北战南征 提交于 2019-11-28 17:41:18
I have a table variable in a script (not a stored procedure). Two questions: How do I drop the table variable? Drop Table @varName gives an "Incorrect snytax" error. Should I always do this? I hear it's a good practice. Is it ever really necessary for small scripts like this? Here's my code: Declare @projectList table( name varchar(40) NOT NULL); Insert Into @projectList Values ('BCR-00021') Select * From @projectList Drop Table @projectList -- does not work Table variables are automatically local and automatically dropped -- you don't have to worry about it. Table variables are just like int

Table variable poor performance on insert in SQL Server Stored Procedure

≡放荡痞女 提交于 2019-11-28 11:13:48
We are experiencing performance problems using a table variable in a Stored Procedure. Here is what actually happens : DECLARE @tblTemp TABLE(iId_company INT) INSERT INTO @tblTemp(iId_company) SELECT id FROM ..... The SELECT returns 138 results, but inserting in the TABLE variable takes 1min15 but when I use a temp table with the same SELECT, woops, takes 0sec : CREATE TABLE #temp (iId_company INT) INSERT INTO #temp(iId_company) SELECT id FROM ... What could cause the behavior ? Use a temporary table. You will see much better performance. A detailed explanation for the reasoning behind this is

How can I reseed an identity column in a T-SQL table variable?

混江龙づ霸主 提交于 2019-11-28 06:16:30
I have a T-SQL table variable (not a table) which has an auto incrementing identity column. I want to clear all data from this variable and reset the identity column value to 1. How can this be done? If you're using a table variable, you can't do it. If it were a table, you could truncate it or use DBCC CHECKIDENT . But, if you have to use a table variable, you have to use something other than an identity column. Or, more accurately, use the identity column in your table variable but output using ROWNUMBER : DECLARE @t table (pkint int IDENTITY(1,1), somevalue nvarchar(50)) INSERT INTO @t

Can I loop through a table variable in T-SQL?

心已入冬 提交于 2019-11-28 02:58:57
Is there anyway to loop through a table variable in T-SQL? DECLARE @table1 TABLE ( col1 int ) INSERT into @table1 SELECT col1 FROM table2 I use cursors as well, but cursors seem less flexible than table variables. DECLARE cursor1 CURSOR FOR SELECT col1 FROM table2 OPEN cursor1 FETCH NEXT FROM cursor1 I would like to be able to use a table variable in the same manner as a cursor. That way I could execute some query on the table variable in one part of the procedure, and then later execute some code for each row in the table variable. Any help is greatly appreciated. Add an identity to your

How to fix “Must declare the scalar variable” error when referencing table variable?

久未见 提交于 2019-11-27 22:44:51
I can't figure out why (or maybe you just can't do this) I get the out of scope error Must declare the scalar variable "@CompanyGroupSites_Master. So is it that I cannot access my Table variable this way inside my Cursor, or I must have missed something simple that's keeping that table variable out of scope when referencing from within the cursor body? DECLARE @TotalCompaniesToProcess int SET @TotalCompaniesToProcess = (select distinct Count(BusinessLine) from vwBuisinessUnit) IF(@TotalCompaniesToProcess > 0) BEGIN ---------------- ############# SETUP ############# ---------------- DECLARE

PostgreSQL table variable

删除回忆录丶 提交于 2019-11-27 21:17:47
Is there anything like table variables in T-SQL? In Sql Server it looks like this: DECLARE @ProductTotals TABLE ( ProductID int, Revenue money ) Then in procedure I can: INSERT INTO @ProductTotals (ProductID, Revenue) SELECT ProductID, SUM(UnitPrice * Quantity) FROM [Order Details] GROUP BY ProductID And manipulate with this variable like an ordinary table. Here is description: http://odetocode.com/Articles/365.aspx As @Clodoaldo commented : use a temporary table in PostgreSQL. For your example: CREATE TEMP TABLE product_totals ( product_id int , revenue money ); More information in the manual

How do I drop table variables in SQL-Server? Should I even do this?

♀尐吖头ヾ 提交于 2019-11-27 10:37:24
问题 I have a table variable in a script (not a stored procedure). Two questions: How do I drop the table variable? Drop Table @varName gives an "Incorrect snytax" error. Should I always do this? I hear it's a good practice. Is it ever really necessary for small scripts like this? Here's my code: Declare @projectList table( name varchar(40) NOT NULL); Insert Into @projectList Values ('BCR-00021') Select * From @projectList Drop Table @projectList -- does not work 回答1: Table variables are

SELECT INTO a table variable in T-SQL

这一生的挚爱 提交于 2019-11-27 10:03:40
Got a complex SELECT query, from which I would like to insert all rows into a table variable, but T-SQL doesn't allow it. Along the same lines, you cannot use a table variable with SELECT INTO or INSERT EXEC queries. http://odetocode.com/Articles/365.aspx Short example: declare @userData TABLE( name varchar(30) NOT NULL, oldlocation varchar(30) NOT NULL ) SELECT name, location INTO @userData FROM myTable INNER JOIN otherTable ON ... WHERE age > 30 The data in the table variable would be later used to insert/update it back into different tables (mostly copy of the same data with minor updates).

Table variable poor performance on insert in SQL Server Stored Procedure

拜拜、爱过 提交于 2019-11-27 06:00:58
问题 We are experiencing performance problems using a table variable in a Stored Procedure. Here is what actually happens : DECLARE @tblTemp TABLE(iId_company INT) INSERT INTO @tblTemp(iId_company) SELECT id FROM ..... The SELECT returns 138 results, but inserting in the TABLE variable takes 1min15 but when I use a temp table with the same SELECT, woops, takes 0sec : CREATE TABLE #temp (iId_company INT) INSERT INTO #temp(iId_company) SELECT id FROM ... What could cause the behavior ? 回答1: Use a