问一问题,得到答案马上结贴

wofan 2004-09-29 10:38:04
数据库里有一表StockData

我要检测它是否存在,如果不存在则创建它

CREATE TABLE [StockData] (
[序号] [int] IDENTITY (1, 1) NOT NULL ,
[仓库] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[日期] [datetime] NULL ,
[配件代码] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[实际库存] [decimal](18, 0) NULL ,
[帐面库存] [decimal](18, 0) NULL ,
[入库数量] [decimal](18, 0) NULL ,
[入库金额] [decimal](18, 0) NULL ,
[出库数量] [decimal](18, 0) NULL ,
[出库成本金额] [decimal](18, 0) NULL ,
[出库销售金额] [decimal](18, 0) NULL ,
[采购入库数量] [decimal](18, 0) NULL ,
[采购入库金额] [decimal](18, 0) NULL ,
[调拨入库数量] [decimal](18, 0) NULL ,
[调拨入库金额] [decimal](18, 0) NULL ,
[移库入库数量] [decimal](18, 0) NULL ,
[移库入库金额] [decimal](18, 0) NULL ,
[其它入库数量] [decimal](18, 0) NULL ,
[其它入库金额] [decimal](18, 0) NULL ,
[报溢入库数量] [decimal](18, 0) NULL ,
[报溢入库金额] [decimal](18, 0) NULL ,
[借出归还数量] [decimal](18, 0) NULL ,
[借出归还金额] [decimal](18, 0) NULL ,
[借进数量] [decimal](18, 0) NULL ,
[借进金额] [decimal](18, 0) NULL ,
[维修领料数量] [decimal](18, 0) NULL ,
[维修领料成本金额] [decimal](18, 0) NULL ,
[维修领料销售金额] [decimal](18, 0) NULL ,
[配件销售数量] [decimal](18, 0) NULL ,
[配件销售成本金额] [decimal](18, 0) NULL ,
[配件销售销售金额] [decimal](18, 0) NULL ,
[内部领用数量] [decimal](18, 0) NULL ,
[内部领用成本金额] [decimal](18, 0) NULL ,
[内部领用销售金额] [decimal](18, 0) NULL ,
[调拨出库数量] [decimal](18, 0) NULL ,
[调拨出库成本金额] [decimal](18, 0) NULL ,
[调拨出库销售金额] [decimal](18, 0) NULL ,
[移库出库数量] [decimal](18, 0) NULL ,
[移库出库成本金额] [decimal](18, 0) NULL ,
[移库出库销售金额] [decimal](18, 0) NULL ,
[其它出库数量] [decimal](18, 0) NULL ,
[其它出库成本金额] [decimal](18, 0) NULL ,
[其它出库销售金额] [decimal](18, 0) NULL ,
[报损出库数量] [decimal](18, 0) NULL ,
[报损出库成本金额] [decimal](18, 0) NULL ,
[报损出库销售金额] [decimal](18, 0) NULL ,
[车间借料数量] [decimal](18, 0) NULL ,
[车间借料成本金额] [decimal](18, 0) NULL ,
[车间借料销售金额] [decimal](18, 0) NULL ,
[借出数量] [decimal](18, 0) NULL ,
[借出成本金额] [decimal](18, 0) NULL ,
[借出销售金额] [decimal](18, 0) NULL ,
[借进归还数量] [decimal](18, 0) NULL ,
[借进归还成本金额] [decimal](18, 0) NULL ,
[借进归还销售金额] [decimal](18, 0) NULL ,
CONSTRAINT [PK_StockDate] PRIMARY KEY CLUSTERED
(
[序号]
) ON [PRIMARY]
) ON [PRIMARY]
GO


该如何写检测部分
...全文
86 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
notin 2004-09-29
  • 打赏
  • 举报
回复
if not exists (select * from sysobjects where name='StockData')
begin tran

CREATE TABLE [StockData] (
[序号] [int] IDENTITY (1, 1) NOT NULL ,
......
......
) ON [PRIMARY]
GO
commit tran
zhpsam109 2004-09-29
  • 打赏
  • 举报
回复
select count(*) into mCount from dbo.sysobjects where name='StockData';

if mCount=0 then

......


else

.........
jackeychen0920 2004-09-29
  • 打赏
  • 举报
回复
if exists (select * from sysobjects where id = object_id(N'[dbo].[StockData]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[StockData]
GO
有就删了
再创建
WangZWang 2004-09-29
  • 打赏
  • 举报
回复
if not exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[StockData]')
and OBJECTPROPERTY(id, N'IsUserTable') = 1)
begin
Create table StockData(
......
)
end

GO
netcoder 2004-09-29
  • 打赏
  • 举报
回复
if not exists (select * from dbo.sysobjects where id = object_id(N'StockData') and OBJECTPROPERTY(id, N'IsUserTable') = 1)

CREATE TABLE [StockData] (
[序号] [int] IDENTITY (1, 1) NOT NULL ,
[仓库] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[日期] [datetime] NULL ,
[配件代码] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[实际库存] [decimal](18, 0) NULL ,
[帐面库存] [decimal](18, 0) NULL ,
[入库数量] [decimal](18, 0) NULL ,
[入库金额] [decimal](18, 0) NULL ,
[出库数量] [decimal](18, 0) NULL ,
[出库成本金额] [decimal](18, 0) NULL ,
[出库销售金额] [decimal](18, 0) NULL ,
[采购入库数量] [decimal](18, 0) NULL ,
[采购入库金额] [decimal](18, 0) NULL ,
[调拨入库数量] [decimal](18, 0) NULL ,
[调拨入库金额] [decimal](18, 0) NULL ,
[移库入库数量] [decimal](18, 0) NULL ,
[移库入库金额] [decimal](18, 0) NULL ,
[其它入库数量] [decimal](18, 0) NULL ,
[其它入库金额] [decimal](18, 0) NULL ,
[报溢入库数量] [decimal](18, 0) NULL ,
[报溢入库金额] [decimal](18, 0) NULL ,
[借出归还数量] [decimal](18, 0) NULL ,
[借出归还金额] [decimal](18, 0) NULL ,
[借进数量] [decimal](18, 0) NULL ,
[借进金额] [decimal](18, 0) NULL ,
[维修领料数量] [decimal](18, 0) NULL ,
[维修领料成本金额] [decimal](18, 0) NULL ,
[维修领料销售金额] [decimal](18, 0) NULL ,
[配件销售数量] [decimal](18, 0) NULL ,
[配件销售成本金额] [decimal](18, 0) NULL ,
[配件销售销售金额] [decimal](18, 0) NULL ,
[内部领用数量] [decimal](18, 0) NULL ,
[内部领用成本金额] [decimal](18, 0) NULL ,
[内部领用销售金额] [decimal](18, 0) NULL ,
[调拨出库数量] [decimal](18, 0) NULL ,
[调拨出库成本金额] [decimal](18, 0) NULL ,
[调拨出库销售金额] [decimal](18, 0) NULL ,
[移库出库数量] [decimal](18, 0) NULL ,
[移库出库成本金额] [decimal](18, 0) NULL ,
[移库出库销售金额] [decimal](18, 0) NULL ,
[其它出库数量] [decimal](18, 0) NULL ,
[其它出库成本金额] [decimal](18, 0) NULL ,
[其它出库销售金额] [decimal](18, 0) NULL ,
[报损出库数量] [decimal](18, 0) NULL ,
[报损出库成本金额] [decimal](18, 0) NULL ,
[报损出库销售金额] [decimal](18, 0) NULL ,
[车间借料数量] [decimal](18, 0) NULL ,
[车间借料成本金额] [decimal](18, 0) NULL ,
[车间借料销售金额] [decimal](18, 0) NULL ,
[借出数量] [decimal](18, 0) NULL ,
[借出成本金额] [decimal](18, 0) NULL ,
[借出销售金额] [decimal](18, 0) NULL ,
[借进归还数量] [decimal](18, 0) NULL ,
[借进归还成本金额] [decimal](18, 0) NULL ,
[借进归还销售金额] [decimal](18, 0) NULL ,
CONSTRAINT [PK_StockDate] PRIMARY KEY CLUSTERED
(
[序号]
) ON [PRIMARY]
) ON [PRIMARY]
GO

34,587

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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