i have a SQL DB that only have reader permissions, as so i cant add my only stored proc or functions, and i am looking for a way to complete my mission without getting admin permission (the software only have reader permissions for developers and does not allow full access, that is why I am looking for another way)
What I have is a table that i want to pull records from, but to do this there is a need to do some calculations on the fields to work out lengths.
is there a way in .Net or using an SQL script file that i can run that will accomplish this? Not sure if I need to do all thoughts to get the required results -create a temp table -insert into temp table fields from a table AND the calculated value -WHERE calculated value is less than 100
(now i can do it using CREATE statements but this is no good, as above, there is no DB access to create anything.. so it needs to be some type of dynamic code that cant do both select and functions in one)
here is a sample of the SQL code of what I am typing to get working in .Net code
BEGIN
CREATE TABLE #Listings
(
AddressID varchar(50), Street varchar(50), City varchar(50), State varchar(50),
Zip varchar(50), ZipCalculatedValue Decimal(18,12)
)
INSERT INTO #Listings (AddressID, Street, City, State, Zip, ZipCalculatedValue)
SELECT AddressID, Street, City, State, Zip, Name, bo.CalcZip(@ZIP) AS ZipCalculatedValue
FROM tbl_Addresses
WHERE dbo.dbo.CalcZip(@ZIP) <= @maxZIP
ORDER BY Distance ASC
SELECT *
FROM #Listings
and the function looks like
DECLARE @ZIP FLOAT
SET @ZIPCalc = (@ZIP - 1000) --more code but removed to simplify
SET @ZIPCalc 开发者_如何学Go= (@ZIPCalc * 10) --result above more workings removed
RETURN @ZIPCalc
so in .Net i can connect to the DB and get results, but any idea on how I can use a FUNCTION with a SELECT statement to pull results?
thanks
Use a Common Table Expression to calculate the value.
WITH AddressWithCalc AS
(
SELECT AddressID, Street, City, State, Zip,
((@ZIP - 1000) * 10) AS ZipCalculatedValue
FROM dbo.tbl_Addresses
)
INSERT INTO #Listings (AddressID, Street, City, State, Zip, ZipCalculatedValue)
SELECT *
FROM AddressWithCalc
WHERE ZipCalculatedValue <= @maxZIP
ORDER BY Distance ASC
精彩评论