0

我正在尝试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())

我只想从其他表中选择特定字段,我想从文本框中获取cash, change,trans_date那么我该怎么做呢?

4

3 回答 3

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 ..... 语法插入从您的选择语句返回的结果集,您不能在末尾使用逗号来传递其他值。只需简单地选择您的选择语句中的值,整个返回的结果集将被插入到您的目标表中。

于 2014-03-14T22:34:42.453 回答
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
于 2014-03-14T22:37:08.540 回答
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,从文本框中添加参数,然后执行。

于 2014-03-14T22:38:09.923 回答