Showing posts with label grab. Show all posts
Showing posts with label grab. Show all posts

Friday, March 23, 2012

How do I insert into existing Temp table?

Hi,

How do I insert data into an existing temporary table? Note: Im primarily a .NET programmer who has to do T-SQL to grab data from time to time.

What I am trying to do is this:
1) Put the scores for all the people who have completed a questionnaire into a temporary table called #GroupConfidence.
2) Add on a row at the end that gives an average for each score (ie the last row is an average of the column above).

I need my SP to give me a DataSet that I can throw straight to my .NET reporting engine (I dont want to do any number crunching inside .NET) - that's why I want to add on the 'average' row at the end.

If I do this (below) the temporary table (#GroupConfidence) gets created and the values inserted.

-- Insert the results into the #GroupConfidence table
SELECT RTRIM(UC.FirstName + ' ' + UC.LastName) AS 'FullName',
RP.SubmitID,
RP.GL_Score,
RP.GP_Score,
RP.GPH_Score,
RP.DL_Score,
RP.MP_Score,
RP.Role_MI_Score,
RP.Role_ASXRE_Score,
RP.Role_APRA_Score,
RP.Overall_Score AS 'AllCategories'
INTO #GroupConfidence
FROM RodResultPercentages RP
JOIN #UsersCompleted UC ON UC.SubmitID = RP.SubmitID

My problem is that #GroupConfidence already exists so in fact I have this code below:

CREATE TABLE #GroupConfidence
( [FullName] [varchar] (200) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[SubmitID] [int] NOT NULL,
[GL_Score] [decimal](19, 10) NOT NULL,
[GP_Score] [decimal](19, 10) NOT NULL,
[GPH_Score] [decimal](19, 10) NOT NULL,
[DL_Score] [decimal](19, 10) NOT NULL,
[MP_Score] [decimal](19, 10) NOT NULL,
[Role_MI_Score] [decimal](19, 10) NOT NULL,
[Role_ASXRE_Score] [decimal](19, 10) NOT NULL,
[Role_APRA_Score] [decimal](19, 10) NOT NULL,
[AllCategories] [decimal](19, 10) NOT NULL
)

-- Insert the results into the #GroupConfidence table
SELECT RTRIM(UC.FirstName + ' ' + UC.LastName) AS 'FullName',
RP.SubmitID,
RP.GL_Score,
RP.GP_Score,
RP.GPH_Score,
RP.DL_Score,
RP.MP_Score,
RP.Role_MI_Score,
RP.Role_ASXRE_Score,
RP.Role_APRA_Score,
RP.Overall_Score AS 'AllCategories'
INTO #GroupConfidence
FROM RodResultPercentages RP
JOIN #UsersCompleted UC ON UC.SubmitID = RP.SubmitID

So I get this error: Server: Msg 2714, Level 16, State 1, Line 109
There is already an object named '#GroupConfidence' in the database.

Thanks in advance,

Ian.SELECT .... INTO <NEWTABLE> FROM <ANOTHER TABLE> will create the table and then insert the rows of the SELECT statement. You are getting the error because first you had created the table using "CREATE TABLE" statement then again are using SELECT ... INTO statment to create the table and insert the rows.

If you want to perform CREATE and INSERT operations in two statements then you can execute below statements:

CREATE TABLE #GroupConfidence ...

INSERT INTO #GroupConfidence SELECT RTRIM(UC.FirstName + ' ' + UC.LastName) AS 'FullName',
RP.SubmitID,
RP.GL_Score,
RP.GP_Score,
RP.GPH_Score,
RP.DL_Score,
RP.MP_Score,
RP.Role_MI_Score,
RP.Role_ASXRE_Score,
RP.Role_APRA_Score,
RP.Overall_Score AS 'AllCategories' FROM RodResultPercentages RP
JOIN #UsersCompleted UC ON UC.SubmitID = RP.SubmitID

Hope the above helps you.|||EXCELLENT - I knew it had to be simple. But unless you know it just isn't going to happen.

So thanks for this.

Ian.

:D

Wednesday, March 21, 2012

How do i grab a simple list of SQL Server views, tables or sprocs?

I need just the names of tables, views and sprocs within a SQL Server
database. What's the easiest way to do this?[posted and mailed, please reply in news]

Kofi (kofisarfo@.gmail.com) writes:
> I need just the names of tables, views and sprocs within a SQL Server
> database. What's the easiest way to do this?

SELECT name FROM sysobjects WHERE xtype = 'U' -- Tables
SELECT name FROM sysobjects WHERE xtype = 'V' -- Views
SELECT name FROM sysobjects WHERE xtype = 'P' -- Stored Procedures

--
Erland Sommarskog, SQL Server MVP, sommar@.algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||"Kofi" <kofisarfo@.gmail.com> wrote in message
news:5c157557.0405111038.2cecb2cb@.posting.google.c om...
> I need just the names of tables, views and sprocs within a SQL Server
> database. What's the easiest way to do this?

SELECT table_name AS object_name,
table_schema AS schema_name,
table_type AS object_type
FROM INFORMATION_SCHEMA.TABLES
UNION ALL
SELECT routine_name AS object_name,
routine_schema AS schema_name,
routine_type AS object_type
FROM INFORMATION_SCHEMA.ROUTINES
WHERE routine_type = 'PROCEDURE'
ORDER BY object_type, schema_name, object_name

--
JAG|||Thanks Erland,

I was having a thick moment. I've used those SELECT statements
countless times before. Shame I was looking at the Master database
rather than the database I was in fact interested in.

But now I've a nice alternative below though :)

-- Kofi

Erland Sommarskog <sommar@.algonet.se> wrote in message news:<Xns94E71624A768Yazorman@.127.0.0.1>...
> [posted and mailed, please reply in news]
> Kofi (kofisarfo@.gmail.com) writes:
> > I need just the names of tables, views and sprocs within a SQL Server
> > database. What's the easiest way to do this?
> SELECT name FROM sysobjects WHERE xtype = 'U' -- Tables
> SELECT name FROM sysobjects WHERE xtype = 'V' -- Views
> SELECT name FROM sysobjects WHERE xtype = 'P' -- Stored Procedures