问题
I new transact sql i'm stock with this
CREATE PROCEDURE Example
@MAESTRDESC varchar(50) --Contiene el valor a ingresar por pantalla
AS
/****Vars*****/
DECLARE
@Result TABLE
(
ClientesDisponibles int,
ClientesAgendados int
)
DECLARE
@SQL varchar(500),--Guarda la cantidad de clientes disponibles
@SQLAGENDADOS varchar(500)
SET @SQL = 'SELECT COUNT(idtable) from table';
SET @SQL = 'SELECT COUNT(idtable2) from table2';
INSERT INTO @Result EXEC @SQL, EXEC@SQLAGENDADOS;
SELECT
R.ClientesDisponibles,
R.ClientesAgendados
FROM
@Result R
RETURN
EDIT the exception say's
Column name or number of supplied values does not match table definition.
give's and exception but i don't have any idea about what i'm doing wrong. Thanks
回答1:
You can't chain together multiple EXEC calls like that and you are missing parentheses (and Neil's point too!)
You could do
INSERT INTO @Result(ClientesDisponibles)
EXEC (@SQL)
INSERT INTO @Result(ClientesAgendados)
EXEC (@SQLAGENDADOS)
This will insert 2 separate rows though. No idea why you aren't using sp_executesql for this as per your previous question.
回答2:
It won't work because of this line:
DECLARE @SQL int
You need to declare @SQL as a VARCHAR in order to use it as a string. The same goes for @SQLAGENDADOS.
However, you should also see @Martins answer regarding chaining multiple executes.
Why don't you do the following:
DECLARE @Count1 INT;
DECLARE @Count2 INT;
SELECT @Count1 = COUNT(idtable)
FROM table;
SELECT @Count2 = COUNT(idtable2)
FROM table2;
INSERT INTO @Result
VALUES (@Count1, @Count2);
回答3:
CREATE PROCEDURE Example
@MAESTRDESC varchar(50) --Contiene el valor a ingresar por pantalla
AS
/****Vars*****/
DECLARE
@Result TABLE
(
ClientesDisponibles int,
ClientesAgendados int
)
DECLARE
@SQL int,--Guarda la cantidad de clientes disponibles
@SQLAGENDADOS int
SELECT @SQL = COUNT(idtable) from [table]
SELECT @SQLAGENDADOS = COUNT(idtable2) from [table2]
INSERT @Result VALUES (@SQL, @SQLAGENDADOS)
SELECT
R.ClientesDisponibles,
R.ClientesAgendados
FROM
@Result R
RETURN
来源:https://stackoverflow.com/questions/5899265/where-is-the-problem