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表里面的.