SQL Server 2008 create procedure and create view - sql-server-2008

I was trying to create a view and a procedure. However none of them can be done.
I have tried creating the procedure like this:
create procedure name ( #time )
as
begin
select tag_ID from Location where tag_ID=#time;
end
create procedure name
as
select tag_ID from Location where tag_ID=#time;
end
Both result in the following error
Major Error 0x80040E14, Minor Error 25501
create procedure name
as
select tag_ID from Location where tag_ID=#time
There was an error parsing the query. [ Token line number = 1,Token line offset = 8,Token in error = procedure ]
For creating view
create view Time as select time from Location;
The error I received is:
Major Error 0x80040E14, Minor Error 25501
create view Time as select time from Location
There was an error parsing the query. [ Token line number = 1,Token line offset = 8,Token in error = view ]
It seems to be some problem in using the CREATE, however i cannot figure it out.
I have tried most of the syntax but it seems not working, most of them pop out the same error.
** I am using SQL compact edition.

You're missing the type from the procedure parameter. Try something like this:
CREATE procedure name ( #time varchar(100))
AS BEGIN
select tag_ID from Location where tag_ID=#time;
END

Error Because You are missing datatype for procedure's parameter.
Note : Please recheck which datatype you are using I'm using datetime here.
CREATE PROCEDURE Name (#time DATETIME)
AS
BEGIN
SELECT tag_id
FROM location
WHERE tag_id = #time;
END

Are you using Sql Server Database Engine or SQL CE? I think You are using SQL CE or Limited version in which View and Procedures are not allowed;
Ref
http://social.msdn.microsoft.com/Forums/uk/sqlce/thread/f6ba9114-a962-41c2-b142-448c0f427cce

Related

Creating MySQL Stored Procedure

I am using Aqua Data Studio 20.6 to create a MySQL stored procedure. The following is my procedure, I have named it sp_GetObjIDByProType:
( IN prono char(15), IN imgtype char(8), OUT objid bigint )
BEGIN
SELECT ObjectID INTO objid FROM images WHERE ProNumber = prono AND DocType = imgtype LIMIT 20;
END
When I try to create the stored procedure, I get this error message:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IN imgtype char(8) )
( OUT objid bigint )
BEGIN
SELECT ObjectID INTO objid F' at line 3
However, it appears as if creating the task was actually successfully
(sp_GetObjIDByProType appears under 'Procedures'). I can view and alter this created stored procedure in another window (resulting stored procedure in 'Alter' window). Does this mean that creation of the stored procedure was actually successful, or is there something wrong with what I did?
Looks like your procedure was created. However, the LIMIT 20 in the query does not make sense. You are returning the selected ObjectID in the OUT parameter which can hold only one value. Yet you limit the query to 20 rows.
If you want to return max 20 rows, return the rows as a result set (leave out the out parameter and the INTO objid), or limit the rows to just one row.

select from temporary table after call in mysql procedure

I am trying to make a reusable mysql procedure that takes data from join tables and store that in a temporary table. I will then create different procedures to select/calculate data from the result.
Here is my SQL code so far:
DELIMITER //
CREATE PROCEDURE getModulesForCourseYear(IN cid VARCHAR(10), IN yr INT(1))
CALL getModulesInCourse(cid);
SELECT * FROM modules WHERE year = yr;//
DELIMITER ;
The getModulesInCourse procedure creates a temporary table called modules
After getModulesInCourse is called in the procedure getModulesForCourseYear, I would then like to filter this result. This is where it fails.
I guess this is happening because mysql does not know what table modules is as it does not currently exist.
How would I be able to select from a temporary table in this procedure?
I am doing it in PHPMyAdmin which gives this error:
SQL query:
CREATE PROCEDURE getModulesForCourseYear(IN cid VARCHAR(10), IN yr INT(1))
CALL getModulesInCourse(cid);
SELECT * FROM modules WHERE year = yr;
MySQL said:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT * FROM modules WHERE year = yr' at line 3
NOTE: I am doing the procedure this way as I want to select the same initial data in multiple procedures, then I will filter it down. This way, it prevents duplicate code and if I need to change it later on, I can do so once.

Getting an error while trying to execute MYSQL stored procedure in phpmyadmin

I have created the following table:
CREATE TABLE Toy
(Toy_ID INT NOT NULL AUTO_INCREMENT,
Toy_Name VARCHAR(30) UNIQUE NOT NULL,
Toy_Price NUMERIC NOT NULL,
PRIMARY KEY (Toy_ID)
)
and then I inserted the values in toy table:
INSERT INTO Toy (Toy_Name,Toy_Price)
VALUES ('Naruto',25.00);
INSERT INTO Toy (Toy_Name,Toy_Price)
VALUES ('Goku',25.00);
INSERT INTO Toy (Toy_Name,Toy_Price)
VALUES ('Luffy',25.00);
and then I typed the following stored procedure in SQL window in phpmyadmin:
CREATE PROCEDURE searchtoy (IN toy_no INT)
BEGIN
SELECT * FROM Toy
WHERE Toy_ID = toy_no;
END;
The stored procedure has been created successfully.
Then I tried to execute the stored procedure in the SQL window and I also have added // in the Delimiter text box:
CALL searchtoy(1);
But I am getting the following error:
Error
Static analysis:
1 errors were found during analysis.
Unexpected token. (near ";" at position 17)
SQL query:
CALL searchtoy(1);
MySQL said: Documentation
#1305 - PROCEDURE demo.searchtoy does not exist
Despite the stored procedure being created successfully, it is still showing that the stored procedure does not exist.
Where did I go wrong ?
It would be really helpful if the solution code is provided.
it is looking for searchtoy in demo schema. check the schema in which you have created your function

Trying to create a transaction in phpmyadmin

Trying to create a transaction in phpmyadmin using the routine panel. I want to do an insert and an update:
START TRANSACTION;
INSERT INTO inventoryitems (item, quantity, userid)
VALUES(item, quantity, userid);
UPDATE users
SET cash = cash - (quantity * unitbuyprice);
COMMIT;
You can see the create/edit routine panel in the screen shot below:
Below is the error I get:
The following query has failed: "CREATE DEFINER=root#localhost PROCEDURE InsertInventoryItem(IN item VARCHAR(255), IN quantity INT, IN userid INT, IN unitbuyprice INT) NOT DETERMINISTIC NO SQL SQL SECURITY DEFINER START TRANSACTION; INSERT INTO inventoryitems (item, quantity, userid) VALUES(item, quantity, userid); UPDATE users SET cash = cash - (quantity * unitbuyprice); COMMIT;"
MySQL said: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO inventoryitems (item, quantity, userid) VALUES(item, quantity, user' at line 3
If I remove the Start Transaction, Commit and either the insert or update then the procedure is fine. IE just a single statement works fine but multiple statements always gives an error.
What am I missing when I want to include multiple statements in a procedure.
I have tried with and without the semi colon delimiter.
This stuff just works with MS SQL. I have created Procedures with hundreds of statements inside before.
Cheers for the Help in advance.
I suggest you add BEGIN and END.
Also note:
A local variable should not have the same name as a table column. If an SQL statement ... contains a reference to a column and a declared local variable with the same name, MySQL currently interprets the reference as the name of a variable.
Reference: https://dev.mysql.com/doc/refman/5.7/en/local-variable-scope.html
If we implement control of transaction within the context of a stored program, we should probably also handle an error condition, and issue the rollback within the stored program. (Personally, I adhere to the school of thought that believes we should handle transaction context outside of the stored procedure.)
The procedure definition would look something like this:
DELIMITER $$
CREATE DEFINER=root#localhost PROCEDURE InsertInventoryItem(
IN as_item VARCHAR(255),
IN ai_quantity INT,
IN ai_userid INT,
IN ai_unitbuyprice INT
)
BEGIN
-- handle error conditions by issuing a ROLLBACK and exiting
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
EXIT PROCEDURE;
END;
START TRANSACTION ;
INSERT INTO inventoryitems (item, quantity, userid)
VALUES (as_item, ai_quantity, ai_userid) ;
UPDATE users u
SET u.cash = u.cash - (ai_quantity * ai_unitbuyprice)
WHERE u.userid = ai_userid ;
COMMIT ;
END$$
DELIMITER ;
--
Note that the update will assign a NULL to cash if either ai_quantity or ai_unitbuyprice is NULL. And we probably want a WHERE clause to limit the rows that will be updated. (Without the WHERE clause, the UPDATE statement will update all rows in the table.)
That's what the statements would look like if I wanted to create the procedure from a normal client, such as the mysql command line, or SQLyog.
MySQL syntax is significantly different than Transact-SQL (Microsoft SQL Server). We just have to deal with that.
As far as "this stuff just works with MS SQL", in all fairness, we should be careful to not conflate MySQL itself with the trouble prone idiot-syncracies of the phpMyAdmin client.

How to call a stored procedure using select statement in mysql

I have call statement like
CALL report_procedure
('2013-02-01',now(),'2015-01-01','1');
and i want to use it in a select query.
i have tried like
Select * from ( CALL report_procedure
('2013-02-01',now(),'2015-01-01','1'));
but error occurs.
like
Error Code: 1064. You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near ( CALL report_procedure
('2013-02-01',now(),'2015-01-01','1') at line 3 0.297 sec
Can anyone suggest me a method to call stored procedure in Select statement in mysql??
It is not possible to use result set from procedure in FROM clause. MySQL does not allow doing this.
You may populate another table (or temporary table) in your procedure, and after, use that table in SELECT commands -
CALL report_procedure ('2013-02-01',now(),'2015-01-01','1'); -- fill temp_table
SELECT * FROM temp_table;
--Firstly your store procedure should look something like this:
CREATE PROCEDURE report_procedure(
IN d1 DATE,
dnow DATE,
d2 DATE,
val INT
)
BEGIN SELECT *
FROM yourtablename
WHERE date1 = d1
AND datenow > dnow
AND date2 > d2
AND value = val;
END
--The store procedure contains the select statement.
-- then you can call the store procedure like that:
CALL report_procedure('2013-02-01',now(),'2015-01-01','1');
--hope it helps