如何将存储过程中的值插入一张表 [英] how to insert values from a stored procedure to one table

查看:94
本文介绍了如何将存储过程中的值插入一张表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是sql的新手.我在存储过程中写道要显示表中的特定字段.这些显示的字段需要插入到新表中.如何做到这一点. 这是我的存储过程.这些将显示SP中提到的以下字段.我需要将这些字段插入到另一个新表中...该怎么做?


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?


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



已添加代码标签-LOSMAC [/EDIT]



Code tags added - LOSMAC[/EDIT]

推荐答案

首先,您的SELECT语句不是最佳的!为什么?由于许多原因...

在开始对查询进行编程"之前,您需要了解有关SQL的基本知识.

在这里,您可以获得msdn文章的基本列表:
针对初学者的SQL [ ^ ]
面向初学者的T-SQL挑战 [ ^ ]
加入提示 [查询提示 [表提示 [插入 [存储过程 [交易 [ ^ ]
SQL Server事务和错误处理 [
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[^]


使用SELECT语句来澄清这种情况.由于尚未在select语句中定义任何联接,因此实际上是在执行笛卡尔乘积.这意味着所有三个表中的所有行都将与所有行匹配.

例如,如果表各有3行,则结果集为9行.这很可能不是您所追求的.

请参考
SQL联接 [
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[^]


尝试这个

Hi try this

insert into newtablename(field1,field2..)
select distinct userid,
            fullname,
            lastname,
            firstname,from tblUserProfile 


这篇关于如何将存储过程中的值插入一张表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆