setlocal

call "C:\Program Files\Microsoft Visual Studio .NET Whidbey\Common7\Tools\vsvars32.bat"
rem compile the assemnbly
csc /target:library /out:bin\funcmutate.dll /r:System.Data.dll funcmutate.cs
rem drop the funcions, if they are there
sqlcmd -d Chapter3 -Q "drop FUNCTION Repeat"
sqlcmd -d Chapter3 -Q "drop FUNCTION AddJob"
sqlcmd -d Chapter3 -Q "drop FUNCTION dbo.NEWID"
sqlcmd -d Chapter3 -Q "drop FUNCTION dbo.NEWID2"
sqlcmd -d Chapter3 -Q "drop FUNCTION dbo.Add3"
sqlcmd -d Chapter3 -Q "drop FUNCTION dbo.Add5"
sqlcmd -d Chapter3 -Q "drop FUNCTION dbo.Add6"
sqlcmd -d Chapter3 -Q "drop FUNCTION dbo.Add2"
rem drop the assembly
sqlcmd -d Chapter3 -Q "drop assembly funcmutate"
rem add the assembly as SAFE, first cmd line param is directory of dll
sqlcmd -d Chapter3 -Q "CREATE ASSEMBLY funcmutate FROM '%~1\bin\funcmutate.dll' with PERMISSION_SET = SAFE"
rem add each of the functions

sqlcmd -d Chapter3 -Q "CREATE Function AddJob() returns int EXTERNAL NAME funcmutate:[Chapter3.Functions]::AddJob"
sqlcmd -d Chapter3 -Q "CREATE Function Repeat(@i int, @c nchar(1)) returns nchar(100) EXTERNAL NAME funcmutate:[Chapter3.Functions]::Repeat"
sqlcmd -d Chapter3 -Q "CREATE Function NEWID() returns uniqueidentifier EXTERNAL NAME funcmutate:[Chapter3.Functions]::NEWID"
sqlcmd -d Chapter3 -Q "CREATE Function NEWID2() returns uniqueidentifier EXTERNAL NAME funcmutate:[Chapter3.Functions]::NEWID2"
sqlcmd -d Chapter3 -Q "CREATE Function Add3(@i int) returns int AS EXTERNAL NAME funcmutate:[Chapter3.Functions]::Add3"
sqlcmd -d Chapter3 -Q "CREATE Function Add5(@i int) returns int AS EXTERNAL NAME funcmutate:[Chapter3.Functions]::Add5"
sqlcmd -d Chapter3 -Q "CREATE Function Add6(@i int) returns int AS EXTERNAL NAME funcmutate:[Chapter3.Functions]::Add6"
sqlcmd -d Chapter3 -Q "CREATE Function Add2(@i int) returns int AS EXTERNAL NAME funcmutate:[Chapter3.Functions]::Add2"


rem try default
sqlcmd -d Chapter3 -Q "declare @i int; set @i = dbo.AddJob() ; print @i"
sqlcmd -d Chapter3 -Q "declare @i int; set @i = dbo.AddJob() ; print @i"
sqlcmd -d Chapter3 -Q "declare @i int; set @i = dbo.AddJob() ; print @i"
sqlcmd -d Chapter3 -Q "declare @i int; set @i = dbo.AddJob() ; print @i"

sqlcmd -d Chapter3 -Q "print dbo.Repeat(5, 'A')"


sqlcmd -d Chapter3 -Q "print dbo.NEWID()"
sqlcmd -d Chapter3 -Q "print dbo.NEWID()"
sqlcmd -d Chapter3 -Q "print dbo.NEWID()"
sqlcmd -d Chapter3 -Q "print dbo.NEWID()"

endlocal


    



