求SQL语句

clx333 2004-06-15 10:16:55
我有如下一表结构:
字段1 字段2
A table1
B table1
C table2
... ....
如果我数据库对应的table1 table2...中不包含上表对应的字段1里的记录(A B C...)就将table1,table2...新增A B C...字段
意思是table1 字段(D,E,)变为(A B D E)
   talbe2 字段(H)  变为(C H)
。。。
...全文
102 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
ziqing 2004-06-16
  • 打赏
  • 举报
回复
不是你是想修改表结构还是查询出来用!!
windindance 2004-06-16
  • 打赏
  • 举报
回复
declare @column varchar(50)

while not @column is null
begin
set @column = null
select top 1 @column = field1 from t where not exists(select * From syscolumns where name=field2 and id=object_id(field1)

exec ('ALTER TABLE table1 ADD COLUMN ' + @column + ' varchar(50)')
end
大致是这样,没有测试。
cronuz 2004-06-16
  • 打赏
  • 举报
回复
hehe, 是動態修正数据表結構問題。

数据库是什麼的?SQL Server?
clx333 2004-06-16
  • 打赏
  • 举报
回复
修改表结构
marf_cn 2004-06-15
  • 打赏
  • 举报
回复
没看明白,顶
internetcsdn 2004-06-15
  • 打赏
  • 举报
回复
是alter table

楼主:

你得描述
table1,table2什么时候作为记录值,
什么时候作为数据表才好理解点.

因为table1作为记录值时,对应的相应记录数为不定值,我觉得要写个动态语句,但偏不熟悉,就高手指点...
internetcsdn 2004-06-15
  • 打赏
  • 举报
回复
一楼:

table1,table2是数据表啊.

对于它们得用atler table ..........
internetcsdn 2004-06-15
  • 打赏
  • 举报
回复
看了半天,

终于明白你的意思.

但不会写....

建议到sql server版去问.......
yat5460 2004-06-15
  • 打赏
  • 举报
回复
乱了呵呵
不过你问的问题用left outer join or inner join肯定能完成
最好写成存储过程
CREATE PROCEDURE Code
@strUCode char(8)
AS SELECT Distinct Seal.SState as 名称1,
Seal.Code AS 名称2,
Seal.SName AS 名称3,
FROM Seal INNER JOIN GDUser ON SealData.Code=User.Code
LEFT JOIN Company ON Seal.SNO=Company.SNO
where

User.strUCode=@strUCode

GO

5,939

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧