復制代碼 代碼如下:
ALTER procedure [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
復制代碼 代碼如下:
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SolutionSQLServer"].ToString());
SqlCommand cmd = new SqlCommand("Test",conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@MaxId", SqlDbType.Int).Value = 12000;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
復制代碼 代碼如下:
--·@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.
不過我找到解決辦法,不過很麻煩,
?TOPIC_ID=9328 (第二個回答用 ' sql '進行連接)
(用case end 也行)
4. select into 和 insert into select 兩種復制文句 (這里感謝)
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 --返回上一行代碼受影響的行數
新聞熱點
疑難解答