sqlserver存儲過程的參數
@是標示符,代表是變數
一個@表示局部變數
兩個@表示是全局變數,全局變數是有伺服器定義的
B. mybatis怎麼用sqlserver存儲過程輸出參數
首先定義一個MySQL存儲過程:
DROP PROCEDURE IF EXISTS test ;
CREATE PROCEDURE test (IN p1 VARCHAR(26), OUT pResult VARCHAR(512))
BEGIN
SET pResult := NULL;
SET pResult :=CONCAT ( 'test',p1);
SELECT * FROM tb2 WHERE commet like Concat('%',p1, '%'); -- this 返回一個結果集
END;
tb2的表結構和數據如下:
DROP TABLE IF EXISTS `tb2`;
CREATE TABLE `tb2` (
`t_id` int(10) NOT NULL AUTO_INCREMENT COMMENT '主鍵',
`Name` varchar(255) DEFAULT NULL COMMENT '欄位名',
`type` int(1) DEFAULT NULL COMMENT '類型,0-正常,1-異常,2-傳輸,3-退單',
`commet` varchar(255) DEFAULT NULL COMMENT '備注',
`optime` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '操作時間',
PRIMARY KEY (`t_id`)
) ;
繼續增加新的類:
Stb2.java內容:
package com.springdemo.usermgr.vo;
import java.util.Date;
/**
* tb2表實體類
* @author zhouxj
* @date 2014-09-10 下午03:29:32
*/
public class Stb2{
private Integer t_id;
private String name;
private Integer type;
private String commet;
private java.util.Date optime;
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
public String getCommet() {
return commet;
}
public void setCommet(String commet) {
this.commet = commet;
}
public java.util.Date getOptime() {
return optime;
}
public void setOptime(java.util.Date optime) {
this.optime = optime;
}
}
修改SUserMapper.java內容:
package com.springdemo.usermgr.vo;
import java.util.List;
import java.util.Map;
public interface SUserMapper {
public int insertSUser(SUser user);
public SUser getSUser(String name);
public List<Stb2> getTestProc(Map<String, Object> param);
}
Test2測試類內容:
package domain;
import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.springdemo.usermgr.vo.SUserMapper;
import com.springdemo.usermgr.vo.Stb2;
public class Test2 {
public static void main(String[] args) throws IOException {
String resource = "config.xml";
Reader reader = Resources.getResourceAsReader(resource);
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = ssf.openSession(true); //true 為自動提交事務
try {
Map<String, Object> parms = new HashMap<String, Object>();
parms.put("queryStr", "的");
SUserMapper spMapper = session.getMapper(SUserMapper.class);
List<Stb2> as=spMapper.getTestProc(parms);
String outPara=(String) parms.get("retStr");
System.out.println(outPara);
System.out.println(as.toString());
//org.apache.ibatis.type.JdbcType.VARCHAR
// org.apache.ibatis.mapping.ParameterMode.OUT
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
}
}
}
config.xml配置文件修改內容:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC
"-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<typeAlias alias="SUser" type="com.springdemo.usermgr.vo.SUser" />
<typeAlias alias="Stb2" type="com.springdemo.usermgr.vo.Stb2" />
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root" />
<property name="password" value="pass" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="SUser.xml" />
<mapper resource="Stb2.xml" />
</mappers>
</configuration>
增加配置文件Stb2.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC
"-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.springdemo.usermgr.vo.SUserMapper">
<resultMap type="com.springdemo.usermgr.vo.Stb2" id="itemResult">
</resultMap>
<select id="getTestProc" parameterType="java.util.Map" statementType="CALLABLE"
resultMap="itemResult">
{call test.test(
#{queryStr,jdbcType=VARCHAR,mode=IN},
#{retStr,jdbcType=VARCHAR,mode=OUT})
}
</select>
</mapper>
運行Test2類,可能的結果:
test的
[com.springdemo.usermgr.vo.Stb2@6900bf61, com.springdemo.usermgr.vo.Stb2@3014af22]
返回了參數內容,和兩條記錄。
C. sqlserver存儲過程如何建立可選參數
SQL Server 中的存儲過程(Procere),帶入參數和出參數。
存儲過程(Procere)-基本創建與操作。
--一、無參存儲過程
create procere PTitles
as
select * from titles
go
--2,執行存儲過程
execute PTitles
go
--3,移除存儲過程
--drop procere PTitles
go
5.存儲過程(Procere)-帶入參。
create proc P_Titles_ByType
@type char(12) --入參
as
select * from titles where type=@type
go
--,執行帶參數的存儲過程
--a)方式一
exec P_Titles_ByType @type='business'
go
--b)方式二
exec P_Titles_ByType 'business'
6.存儲過程(Procere)-帶入參和出參。
create proc P_Titles_ByTypeAndPrice
@type char(12), --入參
@price money --入參
as begin
select * from titles
where type=@type and price>@price
end
D. sql server 中 一個要輸入參數和輸出參數的存儲過程。
1、首先我們需要打開SQL Server Managment管理工具,新建一個表。
E. 如何給SQLSERVER存儲過程傳遞數組參數
確切的說不行-SQL SERVER沒有數組類型,ANSI SQL 92標准也不支持數組。但可用其它的方法來實現。 1. You could simulate an array by passing one or more varchar(255) fields with comma-separated values and then use a WHILE loop with PATINDEX and SUBSTR to extract the values. 1、你可以使用幾個VARCHAR(255)欄位來模擬數組,欄位中用逗號分開各個數據,然後使用循環和PATINDEX和SUBSTR分開這些數據。 2. The more usual way to do this would be to populate a temporary table with the values you need and then use the contents of that table from within the stored-procere. Example of this below2、通常這種方法需要為這些數據創建一個臨時表,然後在存儲過程使用表中的內容。如下例create procere mytest @MyParmTempTable varchar(30)asbegin-- @MyParmTempTable contains my parameter list... 這個變數是包含參數的表名-- For simplicity use dynamic sql to into a normal temp table... create table #MyInternalList ( list_item varchar( 2 ) not null)set nocount oninsert #MyInternalList select * from sysobjects create table #MyList ( list_item varchar( 2 ) not null)insert #MyList values ( 'S' ) insert #MyList values ( 'U' ) insert #MyList values ( 'P' )exec mytest "#MyList"3. If all you wanted to do was use the array/list as input to an IN clause in a WHERE statement you could use :-3、如果你想在IN子句里使用輸入的數組參數可以這樣做:CREATE PROCEDURE sp_MyProcere (@MyCommaDelimitedString
F. sqlserver里存儲過程怎麼調用存儲過程
sqlserver里調用存儲過程的具體操作步驟如下:
1、打開SQL Server Managment管理工具,新建一個表。
G. 如何在sqlserver存儲過程中輸出參數,語句是什麼,我不用輸出參數,我只是在體內輸出語句,請問是什麼
在定義時定義一個ouput參數,如以下存儲過程根據時間產生一個唯一ID
CREATE PROCEDURE [getid](@id char(17) OUTPUT)--產生唯一碼
AS
DECLARE @a datetime
select @a=getdate()
DECLARE @time1 char(10)
DECLARE @time2 char(10)
DECLARE @time3 char(10)
DECLARE @time4 char(10)
DECLARE @time5 char(10)
DECLARE @time6 char(10)
DECLARE @time7 char(10)
Select @time1=str(Datename(year,@a))
Select @time2=str(Datename(month,@a))
if Datename(month,@a)<10 select @time2='0'+rtrim(ltrim(@time2))
Select @time3=str(Datename(day,@a))
if Datename(day,@a)<10 select @time3='0'+rtrim(ltrim(@time3))
select @time4=str(Datename(hour,@a))
if Datename(hour,@a)<10 select @time4='0'+rtrim(ltrim(@time4))
Select @time5=str(Datename(minute,@a))
if Datename(minute,@a)<10 select @time5='0'+rtrim(ltrim(@time5))
Select @time6=str(Datename(second,@a))
if Datename(second,@a)<10 select @time6='0'+rtrim(ltrim(@time6))
Select @time7=str(Datename(Millisecond,@a))
if Datename(Millisecond,@a)<10 select @time7='0'+rtrim(ltrim(@time7))
if Datename(Millisecond,@a)<100 select @time7='0'+rtrim(ltrim(@time7))
select @id=ltrim(rtrim(@time1))+ltrim(rtrim(@time2))+ltrim(rtrim(@time3))+ltrim(rtrim(@time4))+ltrim(rtrim(@time5))+ltrim(rtrim(@time6))+ltrim(rtrim(@time7))
GO
在其它存儲過程中用下例語句調用以上這個存儲過程,如下
DECLARE @id char(17)
EXEC [getid] @id OUTPUT
這樣@id就可以得到getid的返回值了
H. sqlserver2008存儲過程的參數有數組類型嗎
您好,沒有數組類型的.所有的參數類型都是systypes表裡面的.