在SQL Server中使用其他参数插入选择

时间:2014-03-14 22:31:22

标签: sql sql-server insert-select

我正在尝试使用其他参数进行Insert Select查询,但它不起作用。以下是我的代码:

INSERT INTO [epmscdc].[dbo].[billing] 
([pid] ,[did] ,[lid] ,[totalamt] ,
[cash] ,[bchange] ,[btrans_date])
SELECT patientlab.did, patientlab.pid, 
       patientlab.lid, laboratory.lprice 
FROM patientlab
INNER JOIN laboratory 
  ON patientlab.lid = laboratory.lid
INNER JOIN doctor 
  ON patientlab.did = doctor.did 
WHERE patientlab.pid = 3 
  AND pstatus = '-', 1000,1000,GETDATE())

我只想从其他表格中选择特定字段,我想从文本框中获取cashchangetrans_date,以便我该怎么做?

3 个答案:

答案 0 :(得分:1)

INSERT INTO [epmscdc].[dbo].[billing] ([pid] ,[did] ,[lid] ,[totalamt] ,[cash] ,[bchange] ,[btrans_date])
SELECT patientlab.did, patientlab.pid, patientlab.lid, laboratory.lprice , 1000,1000,GETDATE()
FROM patientlab
INNER JOIN laboratory ON patientlab.lid = laboratory.lid
INNER JOIN doctor ON patientlab.did = doctor.did 
WHERE patientlab.pid = 3 AND pstatus = '-'

INSERT INTO ..... SELECT .....语法插入从select语句返回的结果集,不能在末尾使用逗号传递其他值。只需在select语句中选择值,整个返回的结果集就会插入到目标表中。

答案 1 :(得分:1)

    INSERT INTO [epmscdc].[dbo].[billing] ([pid] ,[did] ,[lid] ,[totalamt] ,[cash] ,[bchange] ,[btrans_date]) VALUES
(SELECT patientlab.did, patientlab.pid, patientlab.lid, laboratory.lprice,1000,1000,GETDATE() 
            FROM patientlab
            INNER JOIN laboratory ON patientlab.lid = laboratory.lid
            INNER JOIN doctor ON patientlab.did = doctor.did 
            WHERE patientlab.pid = 3 AND pstatus = '-') As b

答案 2 :(得分:1)

INSERT INTO [epmscdc].[dbo].[billing] ([pid] ,[did] ,[lid] ,[totalamt] ,[cash] ,[bchange] ,[btrans_date])
SELECT patientlab.did, patientlab.pid, patientlab.lid, laboratory.lprice,@totalamt,@cash,@btransDate 
FROM patientlab
INNER JOIN laboratory ON patientlab.lid = laboratory.lid
INNER JOIN doctor ON patientlab.did = doctor.did 
WHERE patientlab.pid = 3 AND pstatus = '-'

创建sqlCommand,从文本框中添加参数,然后执行。