我有这个疑问:
SELECT name, lastname
FROM contestant
WHERE name= 'John' AND lastname = 'Smith'
我从上面的查询几个结果,我需要将它们用于以下查询:
SELECT name, lastname,prize, city
FROM draw
WHERE name= name from table contestant AND lastname= name from table contestant
现在,我要建一个表值函数与光标和WHILE这样我就可以有结果的表。
这里是我的尝试,可以请你帮我完成了吗? 这将是对我很大的帮助,以了解这个TSQL章。 谢谢!
CREATE FUNCTION [dbo].[myFunction]
(
@name varchar (44),
@lastname varchar (44)
)
RETURNS
@tmpTable TABLE
(
name char(44),
lastname char(44),
prize varchar(44),
city char(44)
)
AS
BEGIN
DECLARE
/* what do I have to input here */
DECLARE myCursor CURSOR FOR
SELECT name, lastname
FROM contestant
WHERE name= @name AND lastname = @lastname
OPEN myCursor
FETCH NEXT FROM myCursor INTO /* what goes here?*/
WHILE (@@FETCH_STATUS = 0)
BEGIN
-- and here?
FETCH NEXT FROM myCursor INTO /* what goes here?*/
END /*WHILE*/
CLOSE myCursor
DEALLOCATE myCursor
INSERT INTO @tmpTable (name, lastname,prize, city)
SELECT name, lastname,prize, city
FROM prize
WHERE name = @name AND lastname = @lastname
RETURN
END