sqlserver復習
⑴ sqlserver怎麼練習數據,沒有文件
你是用的什麼資料庫,好像有些資料庫有自己自帶的資料庫,有些沒有。不過我們都習慣自己創建資料庫,這樣對數據的欄位比較熟悉點。一般一個公司的資料庫都是保密的,不會公開,專門下載資料庫有點難下,你看看下載一個完整項目,可能會帶資料庫。
SQL是高級的非過程化編輯語言,允許用戶在高層數據結構上工作。它不要求用戶指定對數據的存放方法,也不需要用戶了解具體的數據存放方式,所以具有完全不同底層結構的不同資料庫系統可以使用相同的sql語言作為數據輸入與管理的介面。
⑵ sqlserver利用存儲過程去除重復行的sql語句
還是先上代碼吧
,可以先看
SQL語句去掉重復記錄,獲取重復記錄
復制代碼
代碼如下:
ALTER
procere
[dbo].[PROC_ITEMMASTER_GETUNIQUE]
@PAGEINDEX
INT,@uid
int,@itemnumber
varchar(50)
AS
begin
tran
--開始事務
drop
table
[ItemMaster].[dbo].[testim]
--刪除表
--把不重復記錄轉存到testim中
select
*
into
[ItemMaster].[dbo].[testim]
from
[ItemMaster].[dbo].[dat_item_master]
where
item_uid
in(select
min(item_uid)
as
item_uid
from
[ItemMaster].[dbo].[dat_item_master]
group
by
item_number)
and
status=0
select
top
10
*
from
[ItemMaster].[dbo].[testim]
where
item_uid
not
in
(select
top
(10*(@PAGEINDEX-1))
item_uid
from
[ItemMaster].[dbo].[testim])
and
owneruid=@uid
and
item_number
like
@itemnumber+'%'
--判斷是否出錯
if
@@error<>0
begin
rollback
tran
--出錯則回滾
end
else
begin
--否則提前事務
commit
tran
end
我的數據是這樣的:因為item_uid是標識列,item_number有重復的,
我想過濾成這樣:
順帶說幾個在編程的時候遇到的小問題
1.程序
出現
Could
not
find
stored
procere
找不到這個存儲過程
因為我的程序資料庫有四個,而默認連接是A,但實際要執行B庫里的存儲過程,導致出錯,
解決辦法1:可在A裡面建個一樣的存儲過程2:在執行連接的時候,替換下資料庫就行了
2.
asp.net/C#
將存儲過程中返回的數據集,填充到dataset/datatable
復制代碼
代碼如下:
SqlConnection
conn
=
new
SqlConnection(ConfigurationManager.ConnectionStrings["SolutionSQLServer"].ToString());
SqlCommand
cmd
=
new
SqlCommand("Test",conn);
cmd.CommandType
=
CommandType.StoredProcere;
cmd.Parameters.Add("@MaxId",
SqlDbType.Int).Value
=
12000;
SqlDataAdapter
sda
=
new
SqlDataAdapter(cmd);
DataTable
dt
=
new
DataTable();
sda.Fill(dt);
在這感謝
http://www.cnblogs.com/liujuncm5/archive/2009/08/31/1557569.html
3.在存儲過程裡面,寫SQL語句不能動態不加order
by
功能
比如
復制代碼
代碼如下:
--·@new_orderby
是傳入參數,不能這樣寫
select
top
(10*(2-1))
item_uid
from
testim
order
by
@new_orderby
--執行這個的時候,SQL會出現
The
SELECT
item
identified
by
the
ORDER
BY
number
1
contains
a
variable
as
part
of
the
expression
identifying
a
column
position.
Variables
are
only
allowed
when
ordering
by
an
expression
referencing
a
column
name.
不過我找到解決辦法,不過很麻煩,
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=9328
(第二個回答用
'
sql
'進行連接)
http://databases.aspfaq.com/database/how-do-i-use-a-variable-in-an-order-by-clause.html (用case
end
也行)
4.
select
into
和
insert
into
select
兩種復制文句
(這里感謝http://www.cnblogs.com/freshman0216/archive/2008/08/15/1268316.html)
1.INSERT
INTO
SELECT語句
語句形式為:Insert
into
Table2(field1,field2,...)
select
value1,value2,...
from
Table1
要求目標表Table2必須存在,由於目標表Table2已經存在,所以我們除了插入源表Table1的欄位外,還可以插入常量。
2.SELECT
INTO
FROM語句
語句形式為:SELECT
vale1,
value2
into
Table2
from
Table1
要求目標表Table2不存在,因為在插入時會自動創建表Table2,並將Table1中指定欄位數據復制到Table2中。
5.順便復習下常用的SQL方法語句
復制代碼
代碼如下:
declare
@name
varchar(200)
--聲明變數
set
@name='abcd;def'
--賦值
print
'exec
len
:'+Convert(varchar(10),Len(@name))
--convert(type,value)轉換,Len(value)獲取大小
print
'exec
charindex:'+Convert(varchar(10),CharIndex('e',@name))--CharIndex(find,value)
在value中查找find的位置
print
'not
replace:'+@name
print
'exec
replace:'+Replace(@name,';','')
--用replace替換
print
'exec
substring:'+Substring(@name,0,3)--用substring截取
print
@@RowCount
--返回上一行代碼受影響的行數
作者:chenhuzi
⑶ 開始學習sql server 哪裡下載一些練慣用的表格啊
那就找些示例資料庫唄。
微軟官方有兩種示例資料庫(Samples and Sample Databases )
1、用於Sqlserver2000和Sqlserver2005的【Northwind and pubs Sample Databases for SQL Server 2000】
下載地址:http://www.microsoft.com/downloads/en/details.aspx?FamilyId=06616212-0356-46A0-8DA2-EEBC53A68034&displaylang=en
下載 SQL2000SampleDb.msi 之後,雙擊 SQL2000SampleDb.msi 以提取示例資料庫腳本。SQL2000SampleDb.msi 會將資料庫腳本和自述文件提取到以下默認文件夾:C:\SQL Server 2000 Sample Databases。請按照自述文件中的說明運行安裝腳本。
2、專用於Sqlserver2005 的版本【SQL Server 2005 Samples and Sample Databases 】
下載地址:http://www.microsoft.com/downloads/en/details.aspx?FamilyID=e719ecf7-9f46-4312-af89-6ad8702e4e6e&DisplayLang=en
希望以上信息對你有所幫助。
⑷ 請推薦一本基礎的SQL資料庫的書
《資料庫技術與應用——sql server》我上大學的教課書,感覺挺好的,通俗易懂的。