【问题标题】:sql server 2005 syntax for insertionsql server 2005 插入语法
【发布时间】:2011-03-03 08:49:33
【问题描述】:

我的查询是这样的,我想插入,但使用 SQL Server 2005

insert into postad(cityid,category_id,ad_id,title,ad_description,subcategory_id) 
values((select city_id from city where city_name='mumbai'),
(select cat_id from category where cat_name='automobiles & vehicles'),
(select adid from adType where adtype like'wanted'),'myad','itys grwweefa',
(select subcat_id from  subcategory where category_id =
    (select cat_id from category where cat_name like'automobiles & vehicles')))

谁能帮我实现这个目标

    string city = ddcity.SelectedItem.ToString();
    string cat = ddcat.SelectedItem.ToString();
    string subcat = ddsubcat.SelectedIndex.ToString();
    string adtype = ddadtype.SelectedItem.ToString();
    string title = txttitle.Text.Trim();
    string description = txtdescription.Text.Trim();
    string video = txtvideo.Text.Trim();
    DateTime date = DateTime.Now;
    SqlConnection myConnection = new SqlConnection("Data Source=DELL-PC\\SQLEXPRESS;Initial Catalog=eclass;Persist Security Info=True;integrated security = true");
    string cityq = "select city_id from city where city_name=" + city;
    string catq = "select cat_id from category where cat_name=" + cat;
    string subq = "select subcat_id from city where subcat_name=" + subcat+"and category_id="+catq;

    SqlCommand storead = new SqlCommand("INSERT INTO postad " + "(cityid,category_id,ad_id,title,ad_description,subcategory_id,video,date_creation) " + " values (@city,@cat,@adtype,@title,@description,@subcat,@video,@date)", myConnection);

storead.Parameters.AddWithValue("@title",title);
storead.Parameters.AddWithValue("@city",city);
storead.Parameters.AddWithValue("@cat",cat);
storead.Parameters.AddWithValue("@subcat",subcat);
storead.Parameters.AddWithValue("@adtype",adtype);
storead.Parameters.AddWithValue("@description",description);
storead.Parameters.AddWithValue("@video",video);
storead.Parameters.AddWithValue("@date",date)

这就是它的工作方式 n id 是主键

【问题讨论】:

  • 您当前的查询有什么问题?
  • 它说的语法错误!!!
  • 始终使用您正在使用的 DBMS 标记您的数据库相关问题
  • 哦,请不要费心接受之前对您的任何问题给出的任何答案。那是非常不合适的。
  • @shweta:@Uw Concept 有点讽刺。如果他们帮助您解决了您遇到的问题,您应该返回并接受问题的答案。

标签: c# sql sql-server-2005 ado.net


【解决方案1】:
INSERT INTO postad(
  cityid, category_id, 
  ad_id, title, ad_description, 
  subcategory_id)
VALUES 
  SELECT TOP(1) city_id, cat_id, ad_id, 'myad', 'itys grwweefa', subcat_id
  FROM city, category, adType, subcategory
  WHERE city.city_name='mumbai'
    AND category.cat_name='automobiles & vehicles'
    AND adType.adtype like 'wanted'
    AND subcategory.category_id = category.cat_id

这是未经测试的代码


编辑:将您的 INSERT 语句移动到存储过程中,并将查询中的所有字符串值替换为参数

INSERT INTO postad(
  cityid, category_id, 
  ad_id, title, ad_description, 
  subcategory_id)
VALUES 
  SELECT TOP(1) city_id, cat_id, ad_id, @title, @description, subcat_id
  FROM city, category, adType, subcategory
  WHERE city.city_name=@city
    AND category.cat_name=@subcat
    AND adType.adtype like @adtype
    AND subcategory.category_id = category.cat_id

【讨论】:

【解决方案2】:

我怀疑这会起作用,因为子查询中可能返回多行 - 所有子查询都应返回相同数量的行以插入表中。

问题不是很清楚需要实现什么,请修改。

【讨论】:

  • 在每个内部选择查询中添加前 1 后会起作用吗?
  • 这可能会解决即时错误并允许查询运行,但它是否会产生预期的结果完全是另一回事。
猜你喜欢
  • 1970-01-01
  • 2012-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多