DELIMITER $$
USE `test`$$
DROP PROCEDURE IF EXISTS `p_getAllTablesCount`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `p_getAllTablesCount`()
BEGIN
DECLARE tableName VARCHAR (100);
DECLARE tablesn VARCHAR (100);
DECLARE tableCount INT;
DECLARE stopFlag INT;
DECLARE sqlStr VARCHAR(1000);
-- 注意:请修改数据库名称
DECLARE cursor_name CURSOR FOR SELECT TABLE_NAME FROM information_schematables WHERE table_schema='test';
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET stopFlag=1;
CREATE TABLE IF NOT EXISTS temp_table(table_name VARCHAR(100),table_count VARCHAR(100));
OPEN cursor_name;
REPEAT
FETCH cursor_name INTO tableName;
SET sqlStr = CONCAT('SELECT COUNT(1) into @tableCount FROM ', tableName);
SELECT sqlStr INTO @sqlStr;
-- select @sqlStr;
SELECT @tableCount INTO tableCount;
BEGIN
PREPARE stepInsertIntoTable FROM @sqlStr;
EXECUTE stepInsertIntoTable;
END;
SET sqlStr = CONCAT('insert into temp_table values(''',CONCAT(tableName),''',''',CONCAT(tableCount),''');');
SELECT sqlStr INTO @sqlStr;
BEGIN
PREPARE stepInsertIntoTable FROM @sqlStr;
EXECUTE stepInsertIntoTable;
END;
UNTIL stopFlag END REPEAT;
CLOSE cursor_name;
SELECT table_name,table_count FROM temp_table ORDER BY table_count DESC;
-- PREPARE step FROM @sql1;
-- EXECUTE step;
DROP TABLE temp_table;
END$$
DELIMITER ;
连接数据库,并从数据库中读取数据后并输出!
using System;using SystemCollectionsGeneric;
using SystemLinq;
using SystemText;
using SystemDataSqlClient;
namespace Login
{
class Program
{
static void Main(string[] args)
{
//新建一个数据库连接
using(SqlConnection conn = new SqlConnection(GetConnectString()))
{
connOpen();//打开数据库
//ConsoleWriteLine("数据库打开成功!");
//创建数据库命令
SqlCommand cmd = connCreateCommand();
//创建查询语句
cmdCommandText = "SELECT FROM userinfo";
//从数据库中读取数据流存入reader中
SqlDataReader reader = cmdExecuteReader();
//从reader中读取下一行数据,如果没有数据,readerRead()返回flase
while (readerRead())
{
//readerGetOrdinal("id")是得到ID所在列的index,
//readerGetInt32(int n)这是将第n列的数据以Int32的格式返回
//readerGetString(int n)这是将第n列的数据以string 格式返回
int id = readerGetInt32(readerGetOrdinal("id"));
string name = readerGetString(readerGetOrdinal("name"));
string pwd = readerGetString(readerGetOrdinal("password"));
int age = readerGetInt32(readerGetOrdinal("age"));
string sex = readerGetString(readerGetOrdinal("sex"));
string phone = readerGetString(readerGetOrdinal("phone"));
string address = readerGetString(readerGetOrdinal("Address"));
//格式输出数据
ConsoleWrite("ID:{0},Name:{1},PWD:{2},Age:{3},Sex:{4},Phone{5},Address:{6}\n", id, name, pwd, age, sex, phone, address);
}
}
ConsoleReadKey();
}
//得到一个数据库连接字符串
static string GetConnectString()
{
return "Data Source=(local);Initial Catalog=db1;Integrated Security=SSPI;";
}
}
}
下面是我用PROC实现的,可在VC中运行。
EXEC SQL BEGIN DECLARE SECTION;
char name[]="张三";
char sql[]="insert into MyTable(name) values(:=name)";
EXEC SQL END DECLARE SECTION;
EXEC SQL EXECUTE IMMEDIATE :sql;
第一种:数组
$allbrand = array();
while ($testingrow = mysqli_fetch_array ($testingquery)) {
$allbrand[] = $testingrow[2];
}
第二种:字符串
$allbrand = '';
while ($testingrow = mysqli_fetch_array ($testingquery)) {
$allbrand = $testingrow[2]"<br>";
}
请采纳,谢谢
注释的!
Private Sub CommandButton5_Click()
Dim Conn As New ADODBConnection '定义ADODB连接对象
Dim ConnStr As String '定义连接字符串
Dim xiao As String
xiao = ComDataText
'对于SQL 2008,如果以IP方式访问服务器,必须要启动SQL Server Browser服务,如果是Express版本,要以“IP(计算机名)\实例名”方式访问
ConnStr = txtDataText
Dim Records As New ADODBRecordset '定义ADODB对象的记录集
Dim Sheet As Worksheet '定义工作表
Set Sheet = ThisWorkbookWorksheets("Sheet2") '给变量Sheet赋值,注意:是工作簿中索引为1的那个表(通常一个新的工作簿Sheet1的索引为1)
'SheetName = "Data" '把Sheet名称改为Data
SheetCellsClear '清空表中原有的数据
'连接状态是打开就不在进行Open *** 作
ConnOpen ConnStr
Dim SQLStr As String '要执行的SQL语句
SQLStr = "select from Shift_Code where Club='" + xiao + "'" '可以执行更复杂的SQL语句
RecordsOpen SQLStr, Conn, adOpenStatic, adLockBatchOptimistic '读取SQL查询结果到Records记录集
'RecordsOpen
Dim i, j, TotalRows, TotalColumns As Integer
j = 0
TotalRows = RecordsRecordCount
TotalColumns = RecordsFieldsCount
'下面的循环把表头(即列名)写到Excel表的第一行
For i = 0 To TotalColumns - 1
Sheet2Cells(1, i + 1) = RecordsFields(i)Name
Next
'下面的循环把查询结果写到Excel表中
Do While Not RecordsEOF
For i = 0 To TotalColumns - 1
Sheet2Cells(j + 2, i + 1) = RecordsFields(i)Value
Next
RecordsMoveNext
j = j + 1
Loop
RecordsClose '关闭记录集
ConnClose '关闭连接
Set Records = Nothing '清空对象
Set Conn = Nothing '清空对象
End Sub
有什么不懂的话Hi我!
1、使用set或select直接赋值,变量名以@开头
例如:
set @var=1;
1
可以在一个会话的任何地方声明,作用域是整个会话,称为用户变量。
2、以declare关键字声明的变量,只能在存储过程中使用,称为存储过程变量,例如:
declare var1 int default 0;
1
主要用在存储过程中,或者是给存储传参数中。
两者的区别是:
在调用存储过程时,以declare声明的变量都会被初始化为null。而会话变量(即@开头的变量)则不会被再初始化,在一个会话内,只须初始化一次,之后在会话内都是对上一次计算的结果,就相当于在是这个会话内的全局变量。
主体内容
局部变量
用户变量
会话变量
全局变量
string MyConn = "Data Source=;Initial Catalog=sqlstudent;Integrated Security=True";
SqlConnection MyConnection = new SqlConnection(MyConn);
String str="select from Sc_Student";
SqlDataAdapter sqlada=new SqlDataAdapter(str,MyConn);
DataSet myset=new DataSet();
sqladaFill(myset);
然后你的Sc_Student表里面的内容都会在myset这个数据集中,用String ss=mysetTable[0]Rows[x][y]ToString();取出你想要的值,X,Y为数据表的Row 和cell
以上就是关于mysql 存储过程怎么赋值全部的内容,包括:mysql 存储过程怎么赋值、C#中怎么把sql数据库的一个数据赋值给一个变量、sqlite数据库的字符串如何赋值等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)