首页 > 代码库 > sqlserver学习--1

sqlserver学习--1

1.远程登录方式

   技术分享

2.查询前100条数据

   select top 100 * from [dbo].[Flow_Share_Day_TAX]

3.获取系统时间

 SELECT GETDATE() AS ‘当前时间‘

 获取其他时间:

     DATEPART()    返回代表指定日期的指定日期部分的整数

  SELECT DATEPART(YEAR, GETDATE()) AS ‘年份‘
  SELECT DATEPART(MONTH, GETDATE()) AS ‘月份‘
  SELECT DATEPART(DAY, GETDATE()) AS ‘日‘
  SELECT DATEPART(DW, GETDATE()) AS ‘星期‘
  SELECT DATEPART(WEEK, GETDATE()) AS ‘周‘
  SELECT DATEPART(HOUR, GETDATE()) AS ‘小时‘
  SELECT DATEPART(MINUTE, GETDATE()) AS ‘分钟‘
  SELECT DATEPART(SECOND, GETDATE()) AS ‘秒‘

  技术分享

 

  或者

    DATENAME()   返回代表指定日期的指定日期部分的字符串

  SELECT DATENAME(YEAR,GETDATE()) AS ‘年份‘
  SELECT DATENAME(MONTH,GETDATE()) AS ‘月份‘
  SELECT DATENAME(DAY,GETDATE()) AS ‘日‘
  SELECT DATENAME(DW,GETDATE()) AS ‘星期‘
  SELECT DATENAME(WEEK,GETDATE()) AS ‘周‘
  SELECT DATENAME(HOUR,GETDATE()) AS ‘小时‘
  SELECT DATENAME(MINUTE,GETDATE()) AS ‘分钟‘
  SELECT DATENAME(SECOND,GETDATE()) AS ‘秒‘

  

  比较:                                

   DATEPART 和 DATENAME 函数将 datetime 值的指定部分(年、季度、天、小时等)生成为整数值或 ASCII 字符串。由于 smalldatetime 只能精确到分钟,

   所以在这两个函数中使用 smalldatetime 值时,返回的秒和毫秒部分总是为零。

 

  关于日期和时间的其它函数:

  技术分享

 

4.查看表结构

  (1)只能是表,不能加模式名。否则,报错

  技术分享

  (2)只能是表,不能加模式名。否则,报错

   技术分享

 

5.查看建表语句

技术分享

6.IDENTITY() 函数

[PID] [decimal](18, 0) IDENTITY(1,1) NOT FOR REPLICATION NOT NULL

IDENTITY(1,1)  :表示从1开始,自增+1

 

7.查询数据库中所有表名称

 SELECT name FROM SysObjects Where XType=U ORDER BY Name

技术分享

 

8.表结构查询

 SELECT CASE WHEN col.colorder = 1 THEN obj.name ELSE ‘‘ END AS 表名,

        col.colorder AS 序号 ,
        col.name AS 列名 ,
        ISNULL(ep.[value], ‘‘) AS 列说明 ,
        t.name AS 数据类型 ,
        col.length AS 长度 ,
        ISNULL(COLUMNPROPERTY(col.id, col.name, Scale), 0) AS 小数位数 ,
        CASE WHEN COLUMNPROPERTY(col.id, col.name, IsIdentity) = 1 THEN 1ELSE ‘‘ END AS 标识 ,
        CASE WHEN EXISTS ( SELECT 1
                           FROM dbo.sysindexes si
                           INNER JOIN dbo.sysindexkeys sik ON si.id = sik.id AND si.indid = sik.indid
                           INNER JOIN dbo.syscolumns sc ON sc.id = sik.id AND sc.colid = sik.colid
                           INNER JOIN dbo.sysobjects so ON so.name = si.name AND so.xtype = PK
                           WHERE sc.id = col.id AND sc.colid = col.colid 
               ) THEN 1ELSE ‘‘ END AS 主键 , CASE WHEN col.isnullable = 1 THEN 1ELSE ‘‘ END AS 允许空 , ISNULL(comm.text, ‘‘) AS 默认值 FROM dbo.syscolumns col LEFT JOIN dbo.systypes t ON col.xtype = t.xusertype inner JOIN dbo.sysobjects obj ON col.id = obj.id AND obj.xtype = U AND obj.status >= 0 LEFT JOIN dbo.syscomments comm ON col.cdefault = comm.id LEFT JOIN sys.extended_properties ep ON col.id = ep.major_id AND col.colid = ep.minor_id AND ep.name = MS_Description LEFT JOIN sys.extended_properties epTwo ON obj.id = epTwo.major_id AND epTwo.minor_id = 0 AND epTwo.name = MS_Description WHERE obj.name = Flow_Share_Day_TAX--表名 ORDER BY col.colorder ;

 

sqlserver学习--1