【问题标题】:converting mysql insert statement to sql server insert statement?将mysql插入语句转换为sql server插入语句?
【发布时间】:2012-03-19 00:14:25
【问题描述】:

将 mysql insert 语句转换为 sql server 2008 时出现错误。 因为限制和别名不起作用,任何人都可以告诉我在以下查询中我做错了什么。

insert into account (product_cd, cust_id, open_date,
  last_activity_date, status, open_branch_id,
  open_emp_id, avail_balance, pending_balance)
select  a.prod_cd, c.cust_id, a.open_date, a.last_date, 'ACTIVE',
  e.branch_id, e.emp_id, a.avail, a.pend
from customer c cross join 
 (select b.branch_id, e.emp_id 
  from branch b inner join employee e on e.assigned_branch_id = b.branch_id
  where b.city = 'Woburn' limit 1) e
  cross join
 (select 'CHK' prod_cd, '2000-01-15' open_date, '2005-01-04' last_date,
    1057.75 avail, 1057.75 pend union all
  select 'SAV' prod_cd, '2000-01-15' open_date, '2004-12-19' last_date,
    500.00 avail, 500.00 pend union all
  select 'CD' prod_cd, '2004-06-30' open_date, '2004-06-30' last_date,
    3000.00 avail, 3000.00 pend) a
where c.fed_id = '111-11-1111';

错误

Msg 102, Level 15, State 1, Line 9
Incorrect syntax near 'limit'.
Msg 102, Level 15, State 1, Line 14
Incorrect syntax near 'a'.
Msg 102, Level 15, State 1, Line 24
Incorrect syntax near 'limit'.
Msg 102, Level 15, State 1, Line 29
Incorrect syntax near 'a'.
Msg 102, Level 15, State 1, Line 39
Incorrect syntax near 'limit'.
Msg 102, Level 15, State 1, Line 46
Incorrect syntax near 'a'.
Msg 102, Level 15, State 1, Line 56
Incorrect syntax near 'limit'.
Msg 102, Level 15, State 1, Line 59
Incorrect syntax near 'a'.
Msg 102, Level 15, State 1, Line 69
Incorrect syntax near 'limit'.
Msg 102, Level 15, State 1, Line 74
Incorrect syntax near 'a'.
Msg 102, Level 15, State 1, Line 84
Incorrect syntax near 'limit'.
Msg 102, Level 15, State 1, Line 87
Incorrect syntax near 'a'.
Msg 102, Level 15, State 1, Line 97
Incorrect syntax near 'limit'.
Msg 102, Level 15, State 1, Line 102
Incorrect syntax near 'a'.
Msg 102, Level 15, State 1, Line 112
Incorrect syntax near 'limit'.
Msg 102, Level 15, State 1, Line 119
Incorrect syntax near 'a'.

已编辑:

create table account
 (account_id integer  not null identity,
  product_cd varchar(10) not null,
  cust_id integer  not null,
  open_date date not null,
  close_date date,
  last_activity_date date,
  status char(5) not null check(status in ('ACTIVE','CLOSED','FROZEN')),
  open_branch_id smallint ,
  open_emp_id smallint ,
  avail_balance decimal(10,2),
  pending_balance decimal(10,2),
  constraint fk_product_cd foreign key (product_cd)
    references product (product_cd),
  constraint fk_a_cust_id foreign key (cust_id)
    references customer (cust_id),
  constraint fk_a_branch_id foreign key (open_branch_id)
    references branch (branch_id),
  constraint fk_a_emp_id foreign key (open_emp_id)
    references employee (emp_id),
  constraint pk_account primary key (account_id)
 );

【问题讨论】:

    标签: mysql sql sql-server sql-server-2008


    【解决方案1】:

    变化:

     (select b.branch_id, e.emp_id 
      from branch b inner join employee e on e.assigned_branch_id = b.branch_id
      where b.city = 'Woburn' limit 1) 
    

    有了这个:

    (select TOP 1 b.branch_id, e.emp_id 
      from branch b inner join employee e on e.assigned_branch_id = b.branch_id
      where b.city = 'Woburn')
    

    虽然似乎没有ORDER BY 子句,而且你也做了两个CROSS JOIN,但你确定这会返回你预期的输出吗?

    【讨论】:

    • 我运行了你所建议的,而不是我收到Msg 8152, Level 16, State 14, Line 1 String or binary data would be truncated. The statement has been terminated. 错误。
    • @ViswanathanIyer - 这是一个完全不同的问题,现在它正在运行查询,但无法插入数据,因为您的目标表和查询列之间的数据类型不同。您应该让我们知道您的表格定义
    • 谢谢我插入了 6 个字符的状态,在创建表中我定义为 char((5),我修改它现在可以正常工作了。
    猜你喜欢
    • 2023-03-09
    • 2022-06-11
    • 1970-01-01
    • 1970-01-01
    • 2012-12-25
    • 1970-01-01
    • 2018-09-25
    • 2015-05-08
    • 1970-01-01
    相关资源
    最近更新 更多