Click here to Skip to main content
15,917,528 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am new to sql.i wrote on stored procedure to display a particular fields from my table.these displayed fields need to be inserted to a new table.how this can be done.???
this is my stored procedure.these will display the following fields as mentioned in SP.i need to insert these fields to another new table...how this can be done?


SQL
ALTER Procedure [dbo].[SP_EMPLOYEEDETAILS] 
AS 
BEGIN
SELECT distinct 
            userid,
            fullname,
            lastname,
            firstname,
            localjobtitle,
            employmentstatusid,
            dateofjoining,
            dateofexit,
            siteid,
            email,
            topfunctionid,
            active,
            exitingthecompany,
            lmsroleid,
            locallanguagejobtitle
       FROM 
            tblUserProfile,
            tblreportingto ,
            tblCountry


[EDIT]Code tags added - LOSMAC[/EDIT]
Posted
Updated 10-May-12 1:41am
v2

First of all, your SELECT statement is not optimal! Why? Because of many reasons...

Before you start "programming" queries, you need to get a basic knowlegde about SQL.

Here you got a basic list of msdn articles:
SQL for beginners[^]
T-SQL Challenges for beginners[^]
JOIN hints[^]
QUERY hints[^]
TABLE hints[^]
INSERT[^]
STORED PROCEDURE[^]
And something extra:
TRANSACTIONS[^]
SQL Server Transactions and Error Handling[^]
 
Share this answer
 
Comments
Wendelius 10-May-12 16:25pm    
Good links. 5
Maciej Los 10-May-12 16:39pm    
Thank you ;)
Sandeep Mewara 10-May-12 23:06pm    
My 5!
Maciej Los 11-May-12 1:25am    
Thank you ;)
To clarify the situation with your SELECT statement. Because you haven't defined any joins in the select statement, you're actually performing a cartesian product. This means that all the rows from all the three tables will be matched with all rows.

For example, if the tables have 3 rows each, your result set is 9 rows. Most likely this isn't what you're after.

Please refer to SQL Joins[^]
 
Share this answer
 
Comments
Maciej Los 10-May-12 16:01pm    
I was trying to tell exactly the same thing... ;)
Wendelius 10-May-12 16:25pm    
Yes, I noticed. Just wanted to clarify it a little bit. :)
Sandeep Mewara 10-May-12 23:07pm    
my 5!
Wendelius 11-May-12 0:54am    
Thanks :)
Hi try this

SQL
insert into newtablename(field1,field2..)
select distinct userid,
            fullname,
            lastname,
            firstname,from tblUserProfile 
 
Share this answer
 
try this query as
SQL
SELECT distinct 
            userid,
            fullname,
            lastname,
            firstname,
            localjobtitle,
            employmentstatusid,
            dateofjoining,
            dateofexit,
            siteid,
            email,
            topfunctionid,
            active,
            exitingthecompany,
            lmsroleid,
            locallanguagejobtitle into TestTable 
       FROM 
            tblUserProfile,
            tblreportingto ,
            tblCountry
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900