给提T-SQL问题的朋友的一点建议

internetroot 2007-10-23 09:05:44
当你们提问时你们肯定是遇到了自己暂时无法解决的问题,你期待着别人能帮你解决这个问题。单别人帮你解决问题为你带来方便你也应该让别人回答方便一点。很多人提问就是一两句话,不把问题说清楚。通常提的问题要到有原始数据才能操作,很多人也从来不给原始数据,有些朋友想帮你但是一看到还要自己建表自己输数据都懒得回答了,所以我倡议大家提问时注意几点:
1.多花笔墨把问题描述的清楚点,不要让别人想了半天弄出来的答案结果不是你要的;
2.所使用的表结构和演示数据在提问中全部用SQL语句写出来,如要使用表Class则这样给出结构

create table Users
( userid char(6) primary key not null,
username varchar(16) not null,
sex char(1)
)
[code]
同时还把演示数据的insert语句也写上[code=SQL]
INSERT INTO [Users]([userid], [username], [sex]) VALUES('000001','王','1')
INSERT INTO [Users]([userid], [username], [sex]) VALUES('000002','李','1')
INSERT INTO [Users]([userid], [username], [sex]) VALUES('000003','张','0')


这样别人准备回答你问题时就不必将时间浪费在你描述不清的问题和创建表结构和输入演示数据上了。
...全文
122 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
Software 2007-11-10
  • 打赏
  • 举报
回复
顶~~
晓风残月0110 2007-10-27
  • 打赏
  • 举报
回复
agree 把表的数据以变量的形式给出 up
ZZJ_4Ever 2007-10-27
  • 打赏
  • 举报
回复
fcuandy 2007-10-24
  • 打赏
  • 举报
回复
hehe.

有人贴数据了,我就借用一下. 没人贴数据了,我就随手敲一下.
Limpire 2007-10-24
  • 打赏
  • 举报
回复
agree
fmtmbs 2007-10-24
  • 打赏
  • 举报
回复
好建议阿
Limpire 2007-10-24
  • 打赏
  • 举报
回复
最好创建为临时表,或定义为表变量。
fwacky 2007-10-24
  • 打赏
  • 举报
回复
create table Users
( userid char(6) primary key not null,
username varchar(16) not null,
sex char(1)
)
fwacky 2007-10-24
  • 打赏
  • 举报
回复
agree
sunhonglei2004 2007-10-24
  • 打赏
  • 举报
回复
支持
samfeng_2003 2007-10-24
  • 打赏
  • 举报
回复
支持
cs_oldhorse 2007-10-24
  • 打赏
  • 举报
回复
互相提供方便~
playwarcraft 2007-10-24
  • 打赏
  • 举报
回复
頂。。。。。。
某人說隨手敲敲,現在好像很少來敲了 *_*
lzuyh 2007-10-24
  • 打赏
  • 举报
回复
xiangyu120 2007-10-24
  • 打赏
  • 举报
回复
agree agree
internetroot 2007-10-24
  • 打赏
  • 举报
回复
如果手写Create语句觉得麻烦可以打开企业管理器,找到要的表右击,在弹出菜单中选择复制,然后打开文本编辑器直接粘贴就可以把表结构粘贴出来;
如果觉得手工写成Insert Into 语句麻烦可以使用下面的存储过程自动生成INSERT INTO语句:
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON
GO

CREATE proc pGetInsertSQL (@TableName varchar(256))
as
begin
set nocount on
declare @sqlstr varchar(4000)
declare @sqlstr1 varchar(4000)
declare @sqlstr2 varchar(4000)
select @sqlstr='select ''insert '+@tablename
select @sqlstr1=''
select @sqlstr2=' ('
select @sqlstr1= ' values ( ''+'
select @sqlstr1=@sqlstr1+col+'+'',''+' ,@sqlstr2=@sqlstr2+name +',' from (select case
-- when a.xtype =173 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.length*2+2)+'),'+a.name +')'+' end'
when a.xtype =104 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(1),'+a.name +')'+' end'
when a.xtype =175 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'
when a.xtype =61 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'convert(varchar(23),'+a.name +',121)'+ '+'''''''''+' end'
when a.xtype =106 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.xprec+2)+'),'+a.name +')'+' end'
when a.xtype =62 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(23),'+a.name +',2)'+' end'
when a.xtype =56 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(11),'+a.name +')'+' end'
when a.xtype =60 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(22),'+a.name +')'+' end'
when a.xtype =239 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'
when a.xtype =108 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.xprec+2)+'),'+a.name +')'+' end'
when a.xtype =231 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'
when a.xtype =59 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(23),'+a.name +',2)'+' end'
when a.xtype =58 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'convert(varchar(23),'+a.name +',121)'+ '+'''''''''+' end'
when a.xtype =52 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(12),'+a.name +')'+' end'
when a.xtype =122 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(22),'+a.name +')'+' end'
when a.xtype =48 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(6),'+a.name +')'+' end'
-- when a.xtype =165 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.length*2+2)+'),'+a.name +')'+' end'
when a.xtype =167 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'
else '''NULL'''
end as col,a.colid,a.name
from syscolumns a where a.id = object_id(@tablename) and a.xtype <>189 and a.xtype <>34 and a.xtype <>35 and a.xtype <>36
)t order by colid

select @sqlstr=@sqlstr+left(@sqlstr2,len(@sqlstr2)-1)+') '+left(@sqlstr1,len(@sqlstr1)-3)+')'' from '+@tablename
-- print @sqlstr
exec( @sqlstr)
set nocount off
end

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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