在EXCEL / VBA中我可以编程出雷雨,但在SQL中我仍然是新手。所以道歉,经过大量的谷歌搜索,我只能得到一个解决方案,我认为最终将非常简单,只是没有包围我的头。
我需要创建一个INSERT脚本来在3列表中添加多行。一个简单的插入是:
INSERT INTO table VALUES(StoreId, ItemID, 27)
第一个障碍是为不同表中的每个StoreID动态重复此操作。我认为这就是:
INSERT INTO table
SELECT (SELECT StoreID FROM Directory.Divisions), ItemID, 27)
如果这实际上是正确的并且会为每个商店有效地创建50-60行,那么我几乎就在那里。问题是ItemID。这实际上是我想手动输入的ItemID数组。因此,如果有50个商店和3个ItemID,则会输入150行。类似的东西:
ItemID = (123,456,789,246,135)
那么如何合并这两个想法呢?从另一个表中拉出StoreID,为第二个参数输入项目数组,然后在末尾输入我的硬编码27。 50个商店和10个商品应该创建500行。提前谢谢。
答案 0 :(得分:0)
您可以使用into
插入目标表。要生成itemid,您必须在union all
表格中使用cross join
和divisions
。
select
d.storeid,
x.itemid,
27 as somecolumn
into targettablename
from Directory.Divisions d
cross join (select 123 as itemid union all select 456 union all select 789...) x
编辑:如果尚未创建insert
的表格,则应在插入数据之前创建该表格。
create table targettable as (store_id varchar(20), item_id varchar(20),
somecolumn int);
insert into targettable (store_id, item_id, somecolumn)
select
d.storeid,
x.itemid,
27
from Directory.Divisions d
cross join (select 123 as itemid union all select 456 union all select 789...) x
答案 1 :(得分:0)
首先,您需要在某种表格中使用您的项目ID数组。永久表,表变量或临时表。例如,使用临时表,前缀为哈希符号:
CREATE TABLE #ItemIds (item_id int)
INSERT INTO #ItemIds VALUES (1)
INSERT INTO #ItemIds VALUES (2)
...
INSERT INTO #ItemIds VALUES (10)
然后这应该可以解决问题:
INSERT INTO table
SELECT StoreId, item_Id, 27
FROM Directory.Divisions, #ItemIds
SELECT中设置的结果将被插入'表'。这是笛卡尔联合的一个例子。由于没有连接条件,Directory.Divisions中的每一行都连接到#ItemIds中的每一行。因此,如果您有50个商店和10个商品,那将导致50 x 10 = 500行。
答案 2 :(得分:0)
您可以为商品ID声明表变量,并使用CROSS JOIN将分部记录与项目相乘:http://sqlfiddle.com/#!3/99438/1
protected void Session_Start(object sender, EventArgs e)
{
string id = Session.SessionID;
string[] DomainUserName = HttpContext.Current.User.Identity.Name.Split(new char[] { '\\' });
string svcLocation = System.Configuration.ConfigurationManager.AppSettings["thisWebApiEndpoint"];
string svcParameter= "User/12345";
WebHelper.WebApiBorker.WebApiEndpoint = svcLocation;
var httpClient = WebHelper.WebApiBorker.GetClient();
myMVC.Models.WebApiUserModel UserVM = new myMVC.Models.WebApiUserModel();
//make webapi call to webapi/user
var response = httpClient.GetAsync(svcParameter); //<---what to do after this line
//if(response.IsSuccessStatusCode)
//{
// string content = await response.Content.ReadAsStringAsync();
// UserVM = JsonConvert.DeserializeObject<myMVC.Models.WebApiUserModel>(content);
//}