SQL Server 2012基础知识

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/my_xxh/article/details/81141209

SQL Server 2012基础知识

sql server身份验证,默认登陆名:sa, 密码:在安装时自己设定的

常用的SQL语句

(1)获取数据库名、表名、字段名:

select * from information_schema.columns order by ordinal_position

(2)获取数据表的行数:

select C.name as Owner,B.name as Tabname, A.rows from sys.partitions A 
inner join sys.objects B on A.object_id=B.object_id 
left join sys.schemas C on C.schema_id = B.schema_id
where B.type='U' and  A.rows>0
order by A.rows desc

(3)获取某个数据库中非空表的名称:

--这个根据存储区来判断
select  B.name   from sys.partitions A inner join sys.objects B
on A.object_id=B.object_id 
where B.type='U' and  A.rows>0
--这个根据索引表来判断
select B.name  from sysindexes A inner join sys.objects B
on A.id=B.object_id  
where B.type='U' And A.rows >0

(4)SQL中显示查询结果的前几条记录:

select top 5 * from table;// 返回前5行数据

(5)使用substring函数将字符串’abdcsef’中的‘abd’给提取出来:

括号中数字‘1’表示截取的起始位置是从该字符串第一个字符开始,‘3’表示截取后得到的字符串长度为3个字符。

select substring('abdcsef',1,3)

(6)使用‘charindex’函数定位某个特定字符在字符串(类型为String的字段值)中的位置:

select Results=substring(字段名称,charindex('启始字符',字段名称)+1,charindex('结束字符',字段名称)-charindex('启始字符',字段名称)-1)

(7)将多个字段的查询结果拼接到一起:

扫描二维码关注公众号,回复: 3081087 查看本文章
select a.area,(b.name+b1.name+b2.name) as 'area_name' from users a
left join Area b on b.id = cast(substring(cast(a.area as char),1,2) as int)
left join Area b1 on b1.id = cast(substring(cast(a.area as char),1,4) as int)
left join Area b2 on b2.id = a.area 

字符串连接方式:“||”、 “concat函数”

(8)datetime类型数据”2018-12-30 上午12:49:23”截取日期的年月日:

convert(char(10) ,time , 120)

(9)SQL CASE 多条件用法


Python连接SQL Server

(1)安装 pip

python setup.py install
添加环境变量PATH: C:\Python27\Scripts

(2)安装Pymssql模块

pip install ./pymssql-2.1.3-cp27-cp27m-win32.whl

演示代码,详见[1]

from os import getenv
import pymssql

server = getenv("PYMSSQL_TEST_SERVER")
user = getenv("PYMSSQL_TEST_USERNAME")
password = getenv("PYMSSQL_TEST_PASSWORD")

conn = pymssql.connect(server, user, password, "tempdb")
#conn=pymssql.connect(host='192.168.55.48:63926',user='sa',password='XXXXXX',database='XXXX')
cursor = conn.cursor()
cursor.execute("""
IF OBJECT_ID('persons', 'U') IS NOT NULL
    DROP TABLE persons
CREATE TABLE persons (
    id INT NOT NULL,
    name VARCHAR(100),
    salesrep VARCHAR(100),
    PRIMARY KEY(id)
)
""")
cursor.executemany(
    "INSERT INTO persons VALUES (%d, %s, %s)",
    [(1, 'John Smith', 'John Doe'),
     (2, 'Jane Doe', 'Joe Dog'),
     (3, 'Mike T.', 'Sarah H.')])
# you must call commit() to persist your data if you don't set autocommit to True
conn.commit()

cursor.execute('SELECT * FROM persons WHERE salesrep=%s', 'John Doe')
row = cursor.fetchone()
while row:
    print("ID=%d, Name=%s" % (row[0], row[1]))
    row = cursor.fetchone()

conn.close()

Iterating through results

conn = pymssql.connect(server, user, password, "tempdb")
cursor = conn.cursor()
cursor.execute('SELECT * FROM persons WHERE salesrep=%s', 'John Doe')

for row in cursor:
    print('row = %r' % (row,))

conn.close()

[1] Microsoft® SQL Server® 2012 Express
[2] SQL中显示查询结果的前几条记录
[3] sql server2012怎么设置用户名密码
[4] SQL SERVER 的前世今生–各版本功能对比
[5] SQLEXPR_x64_CHS、SQLEXPRADV_x64_CHS、SQLEXPRWT_x64_CHS、SqlLocalDB、SQLManagementStudio_x64_CHS、SQLServer
[6] python 记一次将数据库查询结果写入到表格经历

猜你喜欢

转载自blog.csdn.net/my_xxh/article/details/81141209