日期:2014-05-17 浏览次数:20435 次
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-10-23 15:57:06
-- Version:
-- Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64)
-- Dec 28 2012 20:23:12
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据:[a]
if object_id('[a]') is not null drop table [a]
go
create table [a]([ID] int,[UserName] int)
insert [a]
select 1,600351
--> 测试数据:[b]
if object_id('[b]') is not null drop table [b]
go
create table [b]([ID] int,[Code] int)
insert [b]
select 1,7708893 union all
select 2,7708894 union all
select 3,7708895 union all
select 4,7708896 union all
select 5,7708897 union all
select 6,7708898
--------------开始查询--------------------------
INSERT INTO C(id, aid , code)
select id,(SELECT username FROM a)aid,code
from [b]
----------------结果----------------------------
/*
*/
CREATE TABLE #UserInfo
(
ID INT
,UserName VARCHAR(20)
)
CREATE TABLE #UserCode
(
ID INT
,Code VARCHAR(20)
)
CREATE TABLE #UserResult
(
ID INT
,AId VARCHAR(20)
,Code VARCHAR(20)
)
INSERT INTO #UserInfo
VALUES(1,'600351')
INSERT INTO #UserCode
SELECT 1, '7708893' UNION ALL
SELECT 2, '7708894' UNION ALL
SELECT 3, &n