Sql server 2000 -Space find - sql-server-2000

This is query:
CREATE TABLE #TempTable(datasize varchar(200))
INSERT #TempTable
EXEC sp_spaceused 'Table1'
When executing this query error message shown as below
Column name or number of supplied
values does not match table definition
How can I solve this problem?

There's a definition mismatch between that temp table and the default output of sp_spaceused.
Use:
CREATE TABLE #TempTable
(
objName varchar(200),
objRows varchar(200),
objReserved varchar(200),
objData varchar(200),
objIndexSize varchar(200),
objUnused varchar(200)
)
INSERT INTO #TempTable
EXEC sp_spaceused 'Table1'

Related

How to insert a dynamic sql variable into a temporary table

So I want to insert the variables email, username, and age from a stored procedure into a temporary table and am running into a few issues.
Declare #email [varchar](8000)
,#user_name [varchar](8000)
, age[int]
select distinct value as value
into #temptable
from #email
,#user_name
,#age
select *
from #temptable
I am getting a syntax error. Does anybody have any advice?
Generally, I would use this syntax to insert the results of a stored procedure into a table or table variable:
INSERT #temptable EXECUTE dbo.MyStoredProcedureName
For example (table variable example):
declare #temptable table
(
name varchar(255),
field varchar(255),
filename varchar(255),
filegroup varchar(255),
size varchar(255),
maxsize varchar(255),
growth varchar(255),
usage varchar(255)
);
INSERT #temptable Exec sp_helpfile;
select * from #temptable ;
EDIT: Per your question about why can't you do this in the SP itself, the syntax would be something like:
DECLARE #Email nvarchar(4000),#UserName nvarchar(4000),#age int
declare #temptable table
(
email nvarchar(4000),
UserName nvarchar(4000),
age int
)
SELECT #Email = 'test#test.com', #UserName = 'MyName', #age = 21
INSERT INTO #temptable(Email, UserName, age) SELECT #Email, #UserName, #age
SELECT * FROM #temptable

Doing a for-each record in a user defined table type in Stored Procedure

:)
I have this defined type:
CREATE TYPE dbo.MyType
AS TABLE
(
name varchar(255),
value varchar(255)
);
Having this stored procedure:
CREATE PROCEDURE MyProcedure #docName varchar(255), #docPath varchar(255), #values AS [dbo].MyType Readonly
AS
declare #ID table (ID int)
INSERT INTO MyTable output inserted.fileID into #ID values (#docName,#docPath)
-- insert loop here
GO;
And the following "one to many" table
CREATE TABLE TableN (
fileID int PRIMARY KEY,
name varchar(255),
value varchar(255)
)
How can I, where it is noted in the above code, make a loop in order to for each record in the MyType table, to insert it into TableN, together with the fileID from the insert?
Thanks!
There's no need for a loop (you need to stop thinking programmatically in SQL and think in datasets). Just do an INSERT:
INSERT INTO TableN (FileID,[name],[value])
SELECT ID.ID,
V.[Name],
V.[value]
FROM #values v
CROSS JOIN #ID ID;

use variable #table in select statement

Can I use a variable as the name of an existing table? The first SELECT works fine, but the second generates an error suggesting I am trying to create a table variable. All I want though is to use a variable for the name of an existing table.
declare #name varchar(100)
CREATE TABLE CURVES(
ARC VARCHAR(10),
RADIUS VARCHAR (10),
CENPTid BIGINT,
StartPTid BIGINT,
EndPTid BIGINT)
INSERT INTO CURVES
VALUES ('10.23', '50.22', 5, 6, 8)
SELECT * FROM CURVES
set #name='CURVES'
SELECT * FROM #name
You cannot do what your trying to do this way.
But you can Declare a Table Variable and then populate that table variable and select from that table variable. something like this...
-- Actual Table
CREATE TABLE CURVES
(
ARC VARCHAR(10),
RADIUS VARCHAR (10),
CENPTid BIGINT,
StartPTid BIGINT,
EndPTid BIGINT
)
GO
-- Populate the table
INSERT INTO CURVES
VALUES ('10.23', '50.22', 5, 6, 8)
GO
-- Declare the table Variable
declare #name TABLE
(
ARC VARCHAR(10),
RADIUS VARCHAR (10),
CENPTid BIGINT,
StartPTid BIGINT,
EndPTid BIGINT
)
INSERT INTO #name --<-- Populate the Table Variable
SELECT * FROM CURVES
SELECT * FROM #name --<-- Now select from table Variable
This is dynamic SQL approach, so
set #name='CURVES'
exec ('SELECT * FROM '+ #name)

Select only few columns from procedure and insert into table

I have a stored procedure that returns 6 columns. But I want to take only 2 columns and insert them into my table variable.
DECLARE #CategoryTable TABLE(
CategoryId Int NOT NULL,
Name nvarchar(255) NOT NULL
)
INSERT INTO #CategoryTable EXEC [GetAllTenantCategories] #TenantId
When I run this
Column name or number of supplied values does not match table
definition
How to insert only specified columns from a stored procedure?
I do not want to use SELECT INTO as it is not supported by SQL Azure
Tried below and got Invalid object name '#Temp'
DECLARE #CategoryTable TABLE(
CategoryId Int NOT NULL,
Name nvarchar(255) NOT NULL
)
INSERT INTO #Temp EXEC [GetAllTenantCategories] 1
INSERT INTO #CategoryTable (CategoryId, Name)
SELECT CategoryId, Name from #Temp
DROP TABLE #Temp
You can create a temp table first and the INSERT the required columns in your table variable.
CREATE TABLE #temp
(
your columns and datatype
)
INSERT INTO #temp
EXEC [GetAllTenantCategories] #TenantId
Then you can,
DECLARE #CategoryTable TABLE(
CategoryId Int NOT NULL,
Name nvarchar(255) NOT NULL
)
INSERT INTO #CategoryTable (CategoryId, Name)
select CategoryId, Name from #temp
Also drop the #temp table,
DROP TABLE #temp
Refer the points taken from https://www.simple-talk.com/sql/performance/execution-plan-basics/
When the Estimated Plan is Invalid
In some instances, the estimated plan won't work at all. For example, try generating an estimated plan for this simple bit of code:
CREATE TABLE TempTable
(
Id INT IDENTITY (1 , 1 )
,Dsc NVARCHAR (50 )
);
INSERT INTO TempTable ( Dsc )
SELECT [Name]
FROM [Sales] .[Store] ;
SELECT *
FROM TempTable ;
DROP TABLE TempTable ;
You will get this error:
Invalid object name 'TempTable'.
The optimizer, which is what is used to generate Estimated Execution plans, doesn't execute T-SQL.
It does run the stateĀ­ments through the algebrizer , the process outlined earlier that is responsible for verifying the names of database objects. Since the query has not yet been executed, the temporary table does not yet exist. This is the cause of the error.
Running this same bit of code through the Actual execution plan will work perfectly fine.
Hope you got why your temp table approach not worked :) Because you might tried as T-SQL
We can use OPENQUERY
SELECT EmployeeID,CurrentSalary INTO #tempEmp
FROM OPENQUERY(LOCALSERVER,'Exec TestDB.dbo.spEmployee')

Inserting into table the values returned from stored procedure

I have a procedure which has a select statement as below:
CREATE PROCEDURE pr_test
as
SELECT * from SOURCETABLE
Can I insert into a temp table by executing the stored procedure pr_test by any chance?
You can use INSERT EXEC
declare #t results (field1 int, ....)
insert #t (field1,...)
exec pr_test
CREATE PROCEDURE pr_test
as
begin
CREATE TABLE #TibetanYaks(
YakID int,
YakName char(30) )
INSERT INTO #TibetanYaks (YakID, YakName)
SELECT YakID, YakName
FROM dbo.Yaks
WHERE YakType = 'Tibetan'
-- Do some stuff with the table
drop table #TibetanYaks
end