Create a user-defined aggregate without SQL CLR - sql-server

I am planning on deploying a database to SQL Azure, so I cannot use the SQL CLR. However, I have a need to create an aggregate function -- in my case, I need to STUnion a bunch of Geography objects together. (Azure is expected to support Spatial by June.)
Is there another way to accomplish this, without making use of the CLR, in a query? Or do I have to create a UDF that will take a table as a parameter and return the aggregate?

You can't do it.

Geography is supported in Azure SQL now.
https://msdn.microsoft.com/en-us/library/cc280766.aspx

Related

MS-Access - Setting parameters for pass-through query

I have a pass-through query (for SQL Server) in Access which works with explicit values. For a non-pass-though query, I just use the visual editor to create parameters for the affected columns.
Is it possible to do the equivalent with a pass-through query or must I assemble the query with VBA (as mentioned in another post)?
TIA,
Paolo
Unfortunately, you cannot. It would be once possible with ODBCDirect workspace but that was removed since Access 2010. Therefore, for pass-through queries, you would have to concatenate SQL.
A possible alternative is to use ADO which allow you to create parameters and thus construct a command and execute it. Note that your project doesn't have to be limited to only DAO; you can use both DAO and ADO, leveraging what works best for your requirements.

Function like USE to point to a SQL database on a different server?

In SQL Server, you can apply the use function to point a query to another database. For example:
USE databasename
GO;
Is there a function that allows you to point to a different database server and use a database on that server? I would expect this to work, but no luck:
USE [servername].databasename
GO;
I know I could just point the query to the database on the other server, but when I am dealing with production versus staging environments, it's more efficient to declare the server and database in the beginning of the query.
Thanks
USE does not span across to another server, you need to define a linked server on your local instance and then you can access data from that server.
I use Linked Servers to accomplish this. I don't know if this will meet your needs, however.
http://msdn.microsoft.com/en-us/library/ms188279.aspx
In Management Studio, this is available under Database/Server Objects/Linked Servers.
You can refer to objects on this server like this:
[Server].database.schema.object
I just realized this isn't what you want. JonH has it right, you can't specify a dabase on another server at the beginning of your query.

SSRS 2008 R2, VS 2008, User Defined Table Types

I have a stored proc that I use to create some customized lists. I'm trying to create it as an SSRS report, but it's running into a sticking point where it chokes trying to deal with the User Defined Table Type parameters.
Am I just screwed?
As an aside, it does work when you call it with a sql exec statement.
Thanks,
AFAIK there is currently no "native" way to pass table-valued parameters from SSRS to a stored procedure. However it is still can be done in a sort of a hackish way:Using Table-Valued Parameters With SQL Server Reporting Services or Passing Multivalued Parameters in SQL Server 2008.

SQL Stored Proc : How to pass a collection of files to a stored proc?

My program has code that saves attachments, I want these attachments to be transferred to the database, and I am going to use a stored procedure to accomplish this.
I need to know what is the #param type to accept an array of binary files?
finally once I have this array, how to I insert this data into a SQL table?
I guess I am looking at using a byte[] for 1 file, but how do I pass from C# or .net a collection of byte arrays to the SP, and what should the param type be to accept this array of byte[]
Updated
Need a solution that will work in 2005 and 2008.
Update
I've decided to scrap the idea of having 1 large SP to process everything. Instead I am going to have smaller SPs, then handle the transaction in .net.
Do you think this would be a better solution, to handle the transaction in .net data objects?
If you were needing only to pass an array of values, I would suggest to format them as XML and pass them as the SQL xml datatype. You can perform select from XML with SQL just as easily as from a table.
It may also technically work if you pass binary data in XML, but I'm not sure it will be a good solution. Anyway, it is an option.
For SQL Server 2008, you can use the table-valued parameter feature.
This allows you to define a parameter of a table type, and then supply values to that parameter as a DataTable from your C# code.
See these blog posts and articles for more information:
SQL Server 2008: Table-valued parameters
Using Table-Valued Parameters in SQL Server 2008
This is a new feature of SQL Server 2008, so you won't have this in SQL Server 2000 or 2005.
UPDATE: if you need to support SQL Server 2005 as well, check out Erland Sommarskog's excellent article Arrays and Lists in SQL Server 2005. It offers a few ideas on how to accomplish this in 2005.
Marc
See Arrays and Lists in SQL 2005. This article aggregates pretty much every technique there is out there and discusses the advantages and problems with each approach.
I decided to create just one SP, and call it multiple times, one for each file I need uploaded, and keep the transaction managed in C# code.

SQL Server Stored Procedure Folders/Grouping

We are currently using SQL Server 2000 but will soon be moving to 2008. I am looking for a way to group related stored procedures into folders. SQL Server 2000 does not seem to have this ability and from my searches it looks like 2008 does not either. This seems like a basic feature that most users would want. Wouldn't it make sense to put all generic stored procedures that are shared across multiple projects in one folder and project specific procs in another?
It seems the way most devs do this now is by using some from of ID_SPNAME syntax and sorting them.
Grouping procs and functions by type in the UI would be nice, but Management Studio can't do it. Using SQL Server 2000, I've done what you suggest (prefixing objects with a grouping code and sorting). In 2005 and 2008, consider creating schemas to serve the same purpose, and grouping your objects in those. Note that the object names are prefixed with the schema name in the UI.
CREATE SCHEMA ShoppingCart AUTHORIZATION Joe
CREATE PROCEDURE AddItem ...
... will display in the UI as ShoppingCart.AddItem.
Schemas in Sql Server 2008
The most common way to do this (in SQL 2005/2008) is by using schemas:
HR.spCalculateEmployeeCompensation
HR.spCalculateContractorBonus
Web.spAppendWebLog
Web.spUserEndSession
Reporting.spGetCurrentYearSummary
Reporting.spGetLastMonthDetail
Not only will these visually organize themselves in the SSMS list, but you can apply different permissions to each schema.

Resources